【发布时间】:2012-02-27 22:16:06
【问题描述】:
大家好,我在将 LIKE 添加到我的 UNION 时遇到困难,它似乎要么返回任何东西,要么只返回 UNION 中指定的项目,具体取决于我喜欢的位置,例如
完整查询
SELECT fruits.key,
region, location, report_date,
inspection_type AS type,
customer AS customer_name,
customer_number, shipper, po
FROM reports
JOIN ( ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `berries` )
UNION ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `melons` )
UNION ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `citrus` )
UNION ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `table_grapes` )
UNION ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `tree_fruit` )
UNION ( SELECT `key` , `report_key`, `shipper`, `po`, `commodity`, `label`, `status`, `location` FROM `lot` )
) fruits
ON inspection_number = fruits.report_key
WHERE fruits.status = '0' OR fruits.status = '1'
ORDER BY report_date DESC
返回
key region location report_date type customer_name customer_number shipper po
17 Great White North South Holywood 2012-10-21 citrus Name 206-420-9564 Yao Ming 4215
16 Great White North Boulder, CO 2012-10-21 citrus Name 206-420-9564 Hanjin 33215
21 nw 1969-12-31 citrus 2 321 sdfg sdfgs
20 nw sdfgsdfg 1969-12-31 citrus 2 321 sdfg sdfg
尝试一下
WHERE fruits.status = '0' OR fruits.status = '1' AND fruits.location LIKE '%Boulder%'
返回完全相同的内容,而不仅仅是位置中带有 Boulder 的行。
另一方面,这将返回该行,但不会返回来自reports 的其余信息
`WHERE fruits.location LIKE '%Bolder%' AND fruits.status = '0' OR fruits.status = '1'
key region location report_date type customer_name customer_number shipper po
16 Great White North Boulder, CO 2012-10-21 citrus Jd Daniel 206-420-9564 Hanjin 33215
这样做的正确方法是什么?我似乎得到了一个的一半,或者一个都没有?
【问题讨论】:
标签: mysql select join union sql-like