mysql - 重写 MySQL 查询并使其简短

标签 mysql

有人可以帮我简化以下 MySQL 查询吗?该查询工作得很好,但我只是想简化查询并使其易于阅读。

SELECT * 
FROM customer_order_payments 
WHERE customer_order_id = (SELECT customer_order_id FROM  customer_order_payments WHERE payment_code = 'O1529P03N001') 
ORDER BY id DESC

表结构如下

+-------------------+--------------+
| customer_order_id | payment_code |
+-------------------+--------------+
|              1528 | O1528P09N002 |
|              1528 | O1528P07N001 |
|              1528 | O1528P09N003 |
|              1529 | O1529P03N001 |
|              1529 | O1529P03N004 |
|              1529 | O1529P03N003 |
|              1529 | O1529P03N002 |
|              1598 | O1598P03N001 |
|              1608 | O1608P03N001 |
|              1610 | O1610P20N001 |
|              1620 | O1620P03N002 |
|              1620 | O1620P03N001 |
|              1634 | O1634P03N003 |
|              1634 | O1634P03N004 |
|              1634 | O1634P03N001 |
|              1634 | O1634P03N002 |
|              1645 | O1645P03N001 |
|              1655 | O1655P02N001 |
|              1663 | O1663P20N001 |
|              1694 | O1694P03N001 |
+-------------------+--------------+

查询将返回 customer_order_id 均为 1529 的行

最佳答案

我认为您可以直接使用 payment_code = '123456789'条件。

SELECT * 
FROM customer_order_payments 
WHERE payment_code = '123456789' 
ORDER BY id DESC

关于mysql - 重写 MySQL 查询并使其简短,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56519949/

相关文章:

mysql - 删除 SQL 表中的额外\in XML 列

MySQL 查询计数来自树状结构的键

php - 在 MySQL 中插入唯一记录

Mysql 服务器在十次查询后就消失了

php - 学说迁移 : altering a column to be nullable in production

mysql - 如何使用C#服务将大量数据从MySQL数据库迁移到SQL Server数据库

php - 如何从 SQL 数据库访问信息到另一台服务器?

php - 如何使用 PHP 从 MySQL 表的一个列中选择多个值

PHP反向存储过程函数?

php - PDO 查询绑定(bind)字符串而不是整数返回结果