dpca31461 2013-09-11 16:40
浏览 184
已采纳

多个连接和条件全部在一起

I've been pulling my hairs for hours that finally I've come to SO to post this question. I read many questions, but it seems that nothing right is happening.

Table 1

a1
a2
a3 <-- linked with b3
a4

Table 2

b1
b2
b3  <-- linked with c3

Table 3

c1
c2
c3  <-- linked with d3

Table 4

d1
d2
d3 
d4

Columns to display with conditions:-

Column: a1 Condition: BETWEEN '2013-9-13' AND '2013-9-14'

Column: a2 Condition: a2 == '2'

Column: a3 Condition: only those results where d3's value is either 1, 2, 3 or 4

I've a PHP string like this "1,2,3,4". d3 should be selected if it is any of the coma separated value. Please also tell how can I use the PHP string for this purpose. In Oracle I used to use 'IN' operator like

'1' IN ('1','2','3')

and it used to solve the problem. But in Mysql I'm pretty confused.

Only those rows should be displayed where all conditions are met.

Thanks

(Note: The question requires a complete SQL query.)

  • 写回答

1条回答 默认 最新

  • dov6891 2013-09-11 17:01
    关注

    I would start with this:

    SELECT
        *
    FROM
        Table1 AS T1
        INNER JOIN Table2 AS T2 ON T1.A3 = T2.B3
        INNER JOIN Table3 AS T3 ON T2.B3 = T3.C3
        INNER JOIN Table4 AS T4 ON T3.C3 = T4.D3
    WHERE
            T1.A1 BETWEEN '2013-09-13' AND '2013-09-14'
        AND T1.A2 = '2'
        AND T4.D3 IN (1, 2, 3, 4)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入