U
U
Urukhayy2015-02-18 10:53:18
MySQL
Urukhayy, 2015-02-18 10:53:18

Is it recommended to make such a selection from the database?

SELECT * FROM table WHERE ID = (SELECT ID FROM second_table WHERE Name="Test")

Or is it better to write the ID once into a variable (on the server), and then choose from it?
And a related question. Is it possible to write the value NOW() to a text cell?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya Konovalov, 2015-02-18
@Urukhayy

1. maybe you meant it?
And if at you selection changes under nested request? Will you have to hardcode the value into a variable again?
You can’t do this, you can cache the result, that’s another story.
2. It is possible to write down NOW () in text. but why only?

K
KIN1991, 2015-02-18
@KIN1991

SELECT * FROM table WHERE ID = (SELECT ID FROM second_table WHERE Name="Test")

See for yourself, the more data you have in the desired table, the longer the query will be executed. It is not good when a subquery is executed with each WHERE iteration. In your case JOIN would be the best solution

I
Igor, 2015-02-18
@hurgadan

There is no single answer to your question. Depends on the size of the database, the presence of indexes, and how often you run such a query.
If the request is made frequently, save the ID and then use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question