mysql - 发送回所有插入或修改行的 sql 请求

标签 mysql sql

我使用mysql。我有 5 个表 table1、table2、table3、table4、table5。每个表有两个字段ins(插入时的时间戳)和mod(修改时的时间戳)。我想找到在 5 个表之一上进行修改或插入的所有 order_no(来自表 table1)。以下请求不起作用(结果只有一行)

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');

所以第一个请求是错误的。你能帮我吗?

最佳答案

如果你注意到

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')

将第一个查询的结果限制为仅表2的插入/修改的记录,这意味着您将表2的插入/修改的行与表3的插入/修改的行与表4的...

只能给出较小的结果。

而第二个查询仅依赖于 table3 给出 200 计数。

关于mysql - 发送回所有插入或修改行的 sql 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59645056/

相关文章:

mysql - CASE 语句未正确添加

php - $wpdb get_results - 从特定类别循环

mysql - 如何使用复杂字符串从mysql获取两个范围之间的数据

java - 如何在 MySQL 表列的一个字段中存储多个值?

sql - 添加具有默认值的列?

php - 使用索引创建 PHP PDO 查询?

PHP 应用程序在云服务器中没有响应

sql - 即使 MySQL 数据库中某些条目的列为空,输出也是 "1"

mysql - 使用 SQL 的返回值作为另一个查询中的参数

用于搜索小写字符串的 SQL 查询