假设我有一张看起来像这样的表格:
+----+--------------+-------+----------------+------------+
| id | product_name | price | bulk_reference | bulk_count |
+----+--------------+-------+----------------+------------+
| 1 | xxxx | 11.99 | 0 | 0 |
+----+--------------+-------+----------------+------------+
| 2 | zzzz | 22.99 | 0 | 0 |
+----+--------------+-------+----------------+------------+
| 3 | | | 2 | 10 |
+----+--------------+-------+----------------+------------+
我可以选择所有产品等,没问题。但是 - 我需要做的是返回所有产品,但行WHERE bulk_reference > 0
需要返回行中未设置的product_name
&的引用行值price
......在同一个结果集中。
因此,例如,我的结果集应如下所示:
[0] => [id] = 1
[product_name] = xxxx
[price] = 11.99
[bulk_reference] = 0
[bulk_count] = 0
[1] => [id] = 2
[product_name] = zzzz
[price] = 22.99
[bulk_reference] = 0
[bulk_count] = 0
[2] => [id] = 3
[product_name] = zzzz
[price] = 22.99
[bulk_reference] = 2
[bulk_count] = 10
我怎样才能只用 MySQL 做到这一点?