我需要从表'category_vouchers'中获得第一行,其ID_new_record列中的值相同,但需要排除分配的= 1
我的查询也得到assigned
= 1
选择cv.id_voucher,nc.id,nc.name作为值,nc.result_text作为resultText,cv.assigned 来自category_vouchers简历 左JOIN category_vouchers cv2开启(cv.id_new_record = cv2.id_new_record和cv.id_voucher> cv2.id_voucher) 在nc.id = cv.id_new_record上内部加入new_record nc cv2.id_voucher在哪里为空
我希望获得id_voucher 7、3和5
答案 0 :(得分:1)
您只需使用 SUBQUERY和JOIN
即可解决该问题select cv.id_voucher, nc.id, nc.name as value, nc.result_text as resultText, cv.assigned
FROM category_vouchers cv
LEFT JOIN (SELECT * FROM category_vouchers WHERE assigned = 0) cv2 ON ( cv.id_new_record = cv2.id_new_record AND cv.id_voucher > cv2.id_voucher)
INNER JOIN new_record nc on nc.id = cv.id_new_record
WHERE cv2.id_voucher IS NULL AND cv.assigned = 0
答案 1 :(得分:0)
请尝试这个
SELECT cv.id_voucher, nc.id, nc.name AS value, nc.result_text AS resultText, cv.assigned
FROM category_vouchers cv
LEFT JOIN category_vouchers cv2 ON (cv.id_new_record = cv2.id_new_record AND cv.id_voucher > cv2.id_voucher)
INNER JOIN new_record nc ON nc.id = cv.id_new_record
WHERE cv2.id_voucher IS NULL AND cv.assigned <> 1
GROUP BY cv.id_new_record
要从sql_mode中删除ONLY_FULL_GROUP_BY
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
答案 2 :(得分:0)
尝试这个
SELECT cv.id_voucher, nc.id, nc.name as value, nc.result_text as resultText, cv.assigned
FROM category_vouchers cv INNER JOIN
(
SELECT id_voucher, MIN(id_voucher) minid
FROM category_vouchers
where assigned != 1
GROUP BY id_voucher
) t ON cv.id_voucher = t.id_voucher AND cv.id_voucher = t.minid
inner join new_record nc on nc.id = cv.id_new_record WHERE cv2.id_voucher IS NULL