patternsqlMinor
Will these queries return the same results?
Viewed 0 times
samethethesereturnwillresultsqueries
Problem
I have this simple query:
And the results are below:
Now I have this query too a bit more complex:
My question is: will the SELECT into the second query return the same data as the data returned by first query? Meaning friend_id = 2250, 4901, 7187, 9337, 9843 or I need to run the first and the programmatically split the results and build the second one? Also I'll like to know which way is more optimum!
SELECT
friend_id
FROM
default_friend
WHERE
user_id = 1And the results are below:
+-----------+
| friend_id |
+-----------+
| 2250 |
| 4901 |
| 7187 |
| 9337 |
| 9843 |
+-----------+Now I have this query too a bit more complex:
SELECT
friend_id, user_id, approved
FROM
default_friend
WHERE
user_id = 1
AND (friend_id IN (SELECT
friend_id
FROM
default_friend
WHERE
user_id = 1))
AND approved = 0My question is: will the SELECT into the second query return the same data as the data returned by first query? Meaning friend_id = 2250, 4901, 7187, 9337, 9843 or I need to run the first and the programmatically split the results and build the second one? Also I'll like to know which way is more optimum!
Solution
No, you don't have to run the first query, get the results, build the second one dynamically and then run it.
And you don't need this complex second query either. The simple one will suffice:
And you don't really need the
So, the question is. Why don't you just keep the original, 1st query, adding only the additional condition (
And you don't need this complex second query either. The simple one will suffice:
SELECT
friend_id, user_id, approved
FROM
default_friend
WHERE
user_id = 1
AND
approved = 0 ;And you don't really need the
user_id and the approved in the SELECT list either. They are going to be 1 and 0 in all rows, aren't they?So, the question is. Why don't you just keep the original, 1st query, adding only the additional condition (
approved = 0) ?:SELECT
friend_id
FROM
default_friend
WHERE
user_id = 1
AND
approved = 0 ;Code Snippets
SELECT
friend_id, user_id, approved
FROM
default_friend
WHERE
user_id = 1
AND
approved = 0 ;SELECT
friend_id
FROM
default_friend
WHERE
user_id = 1
AND
approved = 0 ;Context
StackExchange Database Administrators Q#22682, answer score: 6
Revisions (0)
No revisions yet.