1、SELECT *, 0 AS px
FROM (SELECT a.SMT_id, a.SMT_sortid, a.SMT_cpjm, a.SMT_cpjg, a.SMT_cpjgdw,
a.SMT_cpname, a.SMT_cpjl, a.SMT_date, a.SMT_yp_id, a.SMT_pic,
b.SMT_coname, b.SMT_vip, b.paixu, b.SMT_key, b.SMT_co, b.SMT_qyqq,
b.SMT_addtwo_id, b.SMT_user
FROM [SMT_cp] a INNER JOIN
[SMT_yp] b ON a.SMT_yp_id = b.SMT_id
WHERE charindex('消毒液', a.SMT_cpname) > 0 AND b.SMT_key <> 2) a
WHERE (NOT EXISTS
(SELECT 1
FROM (SELECT a.SMT_id, a.SMT_sortid, a.SMT_cpjm, a.SMT_cpjg, a.SMT_cpjgdw,
a.SMT_cpname, a.SMT_cpjl, a.SMT_date, a.SMT_yp_id, a.SMT_pic,
b.SMT_coname, b.SMT_vip, b.paixu, b.SMT_key, b.SMT_co,
b.SMT_qyqq, b.SMT_addtwo_id, b.SMT_user
FROM [SMT_cp] a INNER JOIN
[SMT_yp] b ON a.SMT_yp_id = b.SMT_id
WHERE charindex('消毒液', a.SMT_cpname) > 0 AND b.SMT_key <> 2)
b
WHERE a.SMT_yp_id = b.SMT_yp_id AND ((a.SMT_date < b.SMT_date) OR
(a.SMT_date = b.SMT_date AND a.SMT_id < b.SMT_id))))
2、
SELECT a.SMT_id, a.SMT_sortid, a.SMT_cpjm, a.SMT_cpjg, a.SMT_cpjgdw,
a.SMT_cpname, a.SMT_cpjl, a.SMT_date, a.SMT_yp_id, a.SMT_pic, b.SMT_coname,
b.SMT_vip, b.paixu, b.SMT_key, b.SMT_co, b.SMT_qyqq, b.SMT_addtwo_id,
b.SMT_user, 1 AS px
FROM [SMT_cp] a INNER JOIN
[SMT_yp] b ON a.SMT_yp_id = b.SMT_id
WHERE charindex('消毒液', a.SMT_cpname) > 0 AND b.SMT_key<>2
ORDER BY px, b.paixu, a.SMT_date DESC
2 个解决方案
#1
SELECT *, 0 AS px
--改為
SELECT a.*, 0 AS px
--就對了
#2
union all
最好把字段名字都写好。
最好把字段名字都写好。
#1
SELECT *, 0 AS px
--改為
SELECT a.*, 0 AS px
--就對了
#2
union all
最好把字段名字都写好。
最好把字段名字都写好。