Py学习  »  DATABASE

mysql:我如何在列中检索两个id相同但值不同的行

Rodrigo • 5 年前 • 1864 次点击  

如何检索列和其他变量中具有相同ID和相同值的两行。这是“数据”表

+----- ------+-----------------+------------------+
| post_id    | meta_key        | meta_value       |
+----- ------+-----------------+------------------+
| 1000       | payment_method  | visa             |
| 1000       | other           | sometext         |
| 1000       | order_total     | 65.00            |
| 1000       | etc             | sometext2        |
| 1001       | payment_method  | bacs             |
| 1001       | other           | sometext         |
| 1001       | order_total     | 105.00           |
| 1001       | etc             | sometext2        |
| 1002       | payment_method  | visa             |
| 1002       | other           | sometext         |
| 1002       | order_total     | 28.00            |
| 1002       | etc             | sometext2        |
| ...        | ...             | ...              |
+------------+-----------------+------------------+

如您所见,付款方式有稳定的价值,订单总额是可变的。

我试过:

从meta_key in('付款方式')的'data'中选择*, 'order_total')按post_id、meta_key分组

输出

+----- ------+-----------------+------------------+
| post_id    | meta_key        | meta_value       |
+----- ------+-----------------+------------------+
| 1000       | payment_method  | visa             |
| 1000       | order_total     | 65.00            |
| 1001       | payment_method  | bacs             |
| 1001       | order_total     | 105.00           |
| 1002       | payment_method  | visa             |
| 1002       | order_total     | 28.00            |
| ...        | ...             | ...              |
+------------+-----------------+------------------+

我只想要支付方式=签证和他各自的邮资。

+----- ------+-----------------+------------------+
| post_id    | meta_key        | meta_value       |
+----- ------+-----------------+------------------+
| 1000       | payment_method  | visa             |
| 1000       | order_total     | 65.00            |
| 1002       | payment_method  | visa             |
| 1002       | order_total     | 28.00            |
| ...        | ...             | ...              |
+------------+-----------------+------------------+

谢谢您。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/48118
 
1864 次点击  
文章 [ 3 ]  |  最新文章 5 年前