我的查询在使用子查询时不起作用
我有商店产品表
"auto_id" "category" "name" "last_update"
"1" "1" "BOOK" "2018-12-06 17:35:19"
"2" "1" "BOOK" "2018-09-26 10:45:08"
"7" "1" "PENCIL" "2018-10-09 08:50:40"
"8" "1" "BOOK" "2018-12-04 14:39:32"
"9" "4" "RULER" "2018-10-11 06:45:50"
"10" "2" "PAPER" "2018-11-13 08:35:34"
"11" "1" "Pen" "2018-11-28 11:54:18"
"12" "2" "ERASER" "2018-11-28 12:06:36"
"13" "2" "PAPER F4" "2018-12-10 16:43:08"
"14" "2" "PAPER F4" "2018-12-05 05:53:30"
"15" "1" "BOOK" "2018-12-10 16:42:37"
SELECT GROUP_CONCAT(DISTINCT(CONCAT('"',z.name,'"'))) AS n
FROM shop_product z
WHERE z.category = 2 AND DATE(z.last_update) BETWEEN '2018-07-01' AND '2018-12-31'
和结果
"PAPER F4","PAPER","ERASER"
以及在没有结果的情况下运行完整查询的地方
SELECT a.id FROM shop_product AS a WHERE a.name IN (
SELECT GROUP_CONCAT(DISTINCT(CONCAT('"',z.name,'"'))) AS n
FROM shop_product z
WHERE z.category = 2 AND DATE(z.last_update) BETWEEN '2018-07-01'AND '2018-12-31')
如果使用结果字符串之前的子查询来更改子查询
SELECT a.id FROM shop_product AS a WHERE a.name IN ("PAPER F4","PAPER","ERASER")
可以显示结果。
如何使用子查询获取数据
答案 0 :(得分:0)
您不需要concat()和group_concat()函数成为垃圾。只需使用简单的子选择即可。
SELECT a.`auto_id`, a.`category`, a.`name`, a.`last_update`
FROM shop_product a
WHERE name IN(
SELECT z.`name`
FROM shop_product z
WHERE z.category = 2
AND DATE(z.last_update)
BETWEEN '2018-07-01' AND '2018-12-31'
)