在 PHP 中,如何使用多个“IN”查询 mySQL 数据库跨不同表的语句?
我想弄清楚如何有两个“IN”语句。
我正在尝试得到这样的东西,尽管我不知道如何使用两个嵌套的选择语句。
$query_row=mysql_query(
"SELECT DISTINCT * FROM table_a WHERE field_1 IN
(SELECT field_1 FROM table_b WHERE field_2 = $field_2),
(SELECT field_3 FROM table_c WHERE field_4 = $field_4)
ORDER BY field_5 DESC
");
如果我删除其中一个 (SELECT...
位,它会起作用,但尝试将它们都放在一起却不起作用。
表 a 在表 B 和 C 中分别有匹配的值:字段 2 和 4。
如何我能让这个发挥作用吗?
I am trying to figure out how to have two "IN" statements.
I'm trying to get something like this, although I don't know how to use both of the nested select statements.
$query_row=mysql_query(
"SELECT DISTINCT * FROM table_a WHERE field_1 IN
(SELECT field_1 FROM table_b WHERE field_2 = $field_2),
(SELECT field_3 FROM table_c WHERE field_4 = $field_4)
ORDER BY field_5 DESC
");
If I remove one of the (SELECT...
bits, it works, but trying to put them both does not.
Table a has matching values in tables B and C: fields 2 and 4 respectively.
How do I get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取决于您是否想测试 IN 之一或两者:
Depending if you want to test if IN either or both:
你可以尝试使用 UNION:
You could try it with a UNION: