问题遇到的现象和发生背景
使用UNION 合并子查询时,子查询带()报错
问题相关代码,请勿粘贴截图
SELECT
goods_kind_id,
shop_unique,
goods_kind_unique,
goods_kind_parunique,
goods_kind_name,
goods_kind_order,
goods_kind_picture,
1 AS same_type,
IFNULL(goods_kind_alias, "") AS goods_kind_alias,
edit_type,
kind_type,
valid_type
FROM
goods_kind
WHERE
shop_unique = "1536215939565"
AND kind_type = 2
AND valid_type = 1
AND goods_kind_parunique IN (
(SELECT
goods_kind_unique
FROM
goods_kind
WHERE
shop_unique = "1536215939565"
AND kind_type = 2
AND goods_kind_parunique = "0"
AND valid_type = 1
)
UNION
(
SELECT
0 AS goods_kind_unique
)
)
ORDER BY
goods_kind_order ASC
运行结果及报错内容
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION
(
SELECT
0 AS goods_kind_unique
)
)
ORDER BY
goods_kind_order' at line 31
我的解答思路和尝试过的方法
去掉 UNION 两侧的()可以运行成功
如下
**SELECT
goods_kind_id,
shop_unique,
goods_kind_unique
FROM
goods_kind
WHERE
shop_unique = "1536215939565"
AND kind_type = 2
AND valid_type = 1
AND goods_kind_parunique IN (
SELECT
goods_kind_unique
FROM
goods_kind
WHERE
shop_unique = "1536215939565"
AND kind_type = 2
AND goods_kind_parunique = "0"
AND valid_type = 1
UNION
SELECT
0 AS goods_kind_unique
)
ORDER BY
goods_kind_order ASC**
我想要达到的结果
为什么加上括号运行不成功,但是单独运行
(SELECT
goods_kind_unique
FROM
goods_kind
WHERE
shop_unique = "1536215939565"
AND kind_type = 2
AND goods_kind_parunique = "0"
AND valid_type = 1
)
UNION
(
SELECT
0 AS goods_kind_unique
)
可以成功