在 PHP 中,如何使用多个“IN”查询 mySQL 数据库跨不同表的语句?

发布于 2024-12-04 07:18:41 字数 470 浏览 0 评论 0原文

我想弄清楚如何有两个“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捂风挽笑 2024-12-11 07:18:41

取决于您是否想测试 IN 之一或两者:

WHERE field_1 IN (SELECT field_1 FROM table_b WHERE field_2 = $field_2) AND/OR field_1 IN (SELECT field_3 FROM table_c WHERE field_4 = $field_4) ...

Depending if you want to test if IN either or both:

WHERE field_1 IN (SELECT field_1 FROM table_b WHERE field_2 = $field_2) AND/OR field_1 IN (SELECT field_3 FROM table_c WHERE field_4 = $field_4) ...
无所的.畏惧 2024-12-11 07:18:41

你可以尝试使用 UNION:

SELECT DISTINCT * 
    FROM table_a 
    WHERE field_1 IN (SELECT field_1 FROM table_b WHERE field_2 = $field_2 
                      UNION 
                      SELECT field_3 FROM table_c WHERE field_4 = $field_4)
    ORDER BY field_5 DESC

You could try it with a UNION:

SELECT DISTINCT * 
    FROM table_a 
    WHERE field_1 IN (SELECT field_1 FROM table_b WHERE field_2 = $field_2 
                      UNION 
                      SELECT field_3 FROM table_c WHERE field_4 = $field_4)
    ORDER BY field_5 DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文