Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL WHERE <multiple-column> IN <subquery>

Is there a way (without JOIN) to use the WHERE clause on 2 columns (OR) IN a subquery? Currently, I'm doing

WHERE 'col1' IN
(
    SELECT id FROM table
) OR 'col2' IN
(
    SELECT id FROM table
)

And I'm sure I can do better :) . i've also tried WHERE ('col1', 'col2') IN <subquery> but MySQL says: Operand should contain 2 column(s)

Thanks for your help.

Edit: By "No join", I mean I'm alreeady making many joins: http://pastebin.com/bRfD21W9, and as you can see, the subqueries are on another table.

like image 541
Max13 Avatar asked Jul 09 '12 10:07

Max13


People also ask

How do I subquery with multiple columns?

If you want compare two or more columns. you must write a compound WHERE clause using logical operators Multiple-column subqueries enable you to combine duplicate WHERE conditions into a single WHERE clause.

Can a subquery return multiple columns in mysql?

SQL: Multiple Column SubqueriesYou can write subqueries that return multiple columns. The following example retrieves the order amount with the lowest price, group by agent code.

How do I SELECT multiple columns in mysql?

To select multiple columns from a table, simply separate the column names with commas! For example, this query selects two columns, name and birthdate , from the people table: SELECT name, birthdate FROM people; Sometimes, you may want to select all columns from a table.

Can a subquery return multiple values?

Multiple-row subqueries are nested queries that can return more than one row of results to the parent query. Multiple-row subqueries are used most commonly in WHERE and HAVING clauses. Since it returns multiple rows, it must be handled by set comparison operators (IN, ALL, ANY).


4 Answers

SELECT *
FROM table

WHERE 
(col_1, col_2) NOT IN
 (SELECT col_1, col_2 FROM table)
like image 138
Avinash Saini Avatar answered Sep 22 '22 00:09

Avinash Saini


Rewrite your code like this and you're good to go:

WHERE ('col1', 'col2') IN
(
    SELECT id, id FROM table
)
like image 32
Matin Kh Avatar answered Sep 23 '22 00:09

Matin Kh


I Read you are not agree with JOIN, but just another way to do it.. See join with friends if it is useful to you..

SELECT `timeline`.`action`, `timeline`.`data`, `timeline`.`tlupdate`,
            u1.`id` AS ufrom_id, u1.`username` AS ufrom_username, u1.`firstname` AS ufrom_firstname, u1.`lastname` AS ufrom_lastname, u1.`picture` AS ufrom_picture,
            u2.`id` AS uto_id, u2.`username` AS uto_username, u2.`firstname` AS uto_firstname, u2.`lastname` AS uto_lastname, u2.`picture` AS uto_picture,
            m.`id` AS m_id, m.`name` AS m_name, m.`alternative_name` AS m_altname, m.`tiny_img` AS m_tiny, m.`normal_img` AS m_normal
    FROM `timeline`
    JOIN `users` u1 ON u1.`id` = `timeline`.`user_id_from`
    JOIN `users` u2 ON u2.`id` = `timeline`.`user_id_to`
    JOIN `friends` f on f.`idol_id`=u1.`id` or f.`idol_id`=u2.`id`
    JOIN `movies` m ON m.`id` = `timeline`.`movie_id`;

Update:

As you are using inner join You can this too to avoid the condition on complete resultSet.

JOIN `friends` f on ((f.`idol_id`=u1.`id` or f.`idol_id`=u2.`id`) and f.idol_id = ?)

Either you can use DISTINCT or use GROUP BY to get unique result.

like image 38
manurajhada Avatar answered Sep 22 '22 00:09

manurajhada


manurajhada answer is good. If you still decide to avoid the JOIN you could:

SELECT ... FROM (SELECT id FROM table) subquery, other_table
WHERE other_table.col1 = subquery.id OR other_table.col2 = subquery.id

This should make MySQL use the join buffer to hold the results of the subquery.

like image 33
MosheElisha Avatar answered Sep 24 '22 00:09

MosheElisha