我和mysql一起工作。我有5张桌子table1,table2,table3,table4,table5。每个表都有两个字段an (插入时的时间戳)和mod (修改时的时间戳)。我想找到所有的order_no (从表table1),其中有修改或插入的5个表之一。下面的请求不起作用(结果只有一行)
select co.order_no
from table1 co
JOIN table2 item
ON co.order_no = item.order_no
JOIN table3 dimension
ON dimension.item_id = item.id
JOIN table4 material
ON material.item_id = item.id
JOIN table5 product
ON product.item_id = item.id
where (co.ins >='2018-12-26 01:00:00' and co.ins <='2019-06-26 01:00:00')
or (co.mod >='2018-12-26 01:00:00' and co.mod <='2019-06-26 01:00:00')
or (item.ins >='2018-12-26 01:00:00' and item.ins <='2019-06-26 01:00:00')
or (item.mod >='2018-12-26 01:00:00' and item.mod <='2019-06-26 01:00:00')
or (dimension.ins >='2018-12-26 01:00:00' and dimension.ins <='2019-06-26 01:00:00')
or (dimension.mod >='2018-12-26 01:00:00' and dimension.mod <='2019-06-26 01:00:00')
or (material.ins >='2018-12-26 01:00:00' and material.ins <='2019-06-26 01:00:00')
or (material.mod >='2018-12-26 01:00:00' and material.mod <='2019-06-26 01:00:00')
or (product.ins >='2018-12-26 01:00:00' and product.ins <='2019-06-26 01:00:00')
or (product.mod >='2018-12-26 01:00:00' and product.mod <='2019-06-26 01:00:00')
order by co.id, item.id DESC;实际上,当我查看维度上的插入或修改时,我有超过200行。
select dimension.id
from table3 dimension
where (dimension.ins >='2018-12-26 01:00:00' and dimension.ins <='2019-06-26 01:00:00')
or (dimension.mod >='2018-12-26 01:00:00' and dimension.mod <='2019-06-26 01:00:00');所以第一个请求是错误的。你能帮我吗?
发布于 2020-01-08 18:48:53
如果你注意到
JOIN table2 item
ON co.order_no = item.order_no随同
or (item.ins >='2018-12-26 01:00:00' and item.ins <='2019-06-26 01:00:00')
or (item.mod >='2018-12-26 01:00:00' and item.mod <='2019-06-26 01:00:00')将第一个查询的结果限制为,只对table2的插入/修改记录进行限制,这意味着您将与表2的插入/修改行与表3的插入/修改行与表4的.
只会给出更小的结果。
而第二个查询只依赖于提供200计数的table3。
https://stackoverflow.com/questions/59645056
复制相似问题