我知道默认情况下不会UNION
生成重复的行。但在这种情况下,因为我使用列sort_col
进行排序,这样会UNION
生成重复的行(实际上不是重复的,因为sort_col
每个行都是不同的select
)。要澄清看到这个答案和评论也如何克服这个?
$key_word_1 = $what;
$key_word_2 = $what."%";
$key_word_3 = "%".$what."%";
(SELECT 1 AS sort_col,
chair.id_user,
chair.company,
chair.district,
chair.summary,
chair.place,
products.name
FROM chair
LEFT JOIN products
ON chair.id_user = products.id_user
WHERE ( chair.district LIKE ?
AND chair.place LIKE ? )
AND ( chair.company LIKE ?
OR chair.summary LIKE ?
OR products.name LIKE ?
OR products.description LIKE ? )
GROUP BY chair.id_user)
UNION
(SELECT 2,
chair.id_user,
chair.company,
chair.district,
chair.summary,
chair.place,
products.name
FROM chair
LEFT JOIN products
ON chair.id_user = products.id_user
WHERE ( chair.district LIKE ?
AND chair.place LIKE ? )
AND ( chair.company LIKE ?
OR chair.summary LIKE ?
OR products.name LIKE ?
OR products.description LIKE ? )
GROUP BY chair.id_user)
UNION
(SELECT 3,
chair.id_user,
chair.company,
chair.district,
chair.summary,
chair.place,
products.name
FROM chair
LEFT JOIN products
ON chair.id_user = products.id_user
WHERE ( chair.district LIKE ?
AND chair.place LIKE ? )
AND ( chair.company LIKE ?
OR chair.summary LIKE ?
OR products.name LIKE ?
OR products.description LIKE ? )
GROUP BY chair.id_user)
ORDER BY sort_col
LIMIT
?, ?