Max*_*x13 23 mysql multiple-tables where-clause in-subquery
有没有办法(没有JOIN)WHERE在2列(OR)IN子查询上使用子句?目前,我正在做
WHERE 'col1' IN
(
SELECT id FROM table
) OR 'col2' IN
(
SELECT id FROM table
)
Run Code Online (Sandbox Code Playgroud)
而且我相信我能做得更好:).我也试过,WHERE ('col1', 'col2') IN <subquery>但MySQL说:Operand should contain 2 column(s)
谢谢你的帮助.
编辑:通过"不加入",我的意思是我正在进行许多加入:http://pastebin.com/bRfD21W9 ,正如您所看到的,子查询位于另一个表上.
小智 24
SELECT *
FROM table
WHERE
(col_1, col_2) NOT IN
(SELECT col_1, col_2 FROM table)
Run Code Online (Sandbox Code Playgroud)
像这样重写你的代码就可以了:
WHERE ('col1', 'col2') IN
(
SELECT id, id FROM table
)
Run Code Online (Sandbox Code Playgroud)
我读到你不同意JOIN,但只是另一种方法。看看join它friends是否对你有用。
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`;
Run Code Online (Sandbox Code Playgroud)
当您使用时,inner join您也可以这样做以避免完整结果集的条件。
JOIN `friends` f on ((f.`idol_id`=u1.`id` or f.`idol_id`=u2.`id`) and f.idol_id = ?)
Run Code Online (Sandbox Code Playgroud)
您可以使用DISTINCT或使用GROUP BY来获得独特的结果。
| 归档时间: |
|
| 查看次数: |
35469 次 |
| 最近记录: |