Ошибка при получении # 1221 - Неправильное использование UNION и LIMIT

103
9

Я пытаюсь сделать запрос объединения с этими двумя запросами, используя union между запросами, но получаю ошибку # 121 Неправильное использование UNION и LIMIT. Пожалуйста, помогите мне сделать это объединение запросов, чтобы получить результат обоих этих запросов.

Query1:

SELECT t1.TraineeID,
t2.attnDate,
MIN(t1.attnTime) AS inTime,
(CASE WHEN MIN(t1.attnTime) < MAX(t1.attnTime) THEN MAX(t1.attnTime) else '' end) AS outTime,
(CASE WHEN t5.Slot ='1' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '1')THEN 'P'
WHEN t5.Slot ='2' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '2')THEN 'P'
WHEN t5.Slot ='3' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '3')THEN 'P'
WHEN t5.Slot ='4' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '4')THEN 'P'
WHEN t5.Slot ='1' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '1')THEN 'Early Out'
WHEN t5.Slot ='2' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '2')THEN 'Early Out'
WHEN t5.Slot ='3' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '3')THEN 'Early Out'
WHEN t5.Slot ='4' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '4')THEN 'Early Out'
ELSE 'Late-In'
END) AS Status

FROM tbl_attn_temp t1, tbl_assigned t3, tbl_batch t5
JOIN (SELECT DISTINCT attnDate FROM tbl_attn_temp) t2
WHERE t1.attndate = t2.attnDate and t3.BatchID=t5.BatchID AND t1.TraineeID = t3.TraineeID
AND t1.attnDate='2016-01-13'
AND(CASE WHEN t5.Slot ='1' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '1') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '1')
WHEN t5.Slot ='2' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '2') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '2')
WHEN t5.Slot ='3' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '3') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '3')
WHEN t5.Slot ='4' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '4') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '4')
END)
GROUP BY t1.TraineeID, t2.attnDate

Query2:

SELECT t1.TraineeID,
t2.attnDate,
MIN(t1.attnTime) AS inTime,
(CASE WHEN MIN(t1.attnTime) < MAX(t1.attnTime) THEN MAX(t1.attnTime) else '' end) AS outTime,
(CASE WHEN t4.Slot ='1' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '1')THEN 'P'
WHEN t4.Slot ='2' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '2')THEN 'P'
WHEN t4.Slot ='3' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '3')THEN 'P'
WHEN t4.Slot ='4' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '4')THEN 'P'
WHEN t4.Slot ='1' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '1')THEN 'Early Out'
WHEN t4.Slot ='2' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '2')THEN 'Early Out'
WHEN t4.Slot ='3' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '3')THEN 'Early Out'
WHEN t4.Slot ='4' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '4')THEN 'Early Out'
ELSE 'Late-In'
END) AS Status

FROM tbl_attn_temp t1, tbl_assigned t3, tbl_instructor_info t4
JOIN (SELECT DISTINCT attnDate FROM tbl_attn_temp) t2
WHERE t1.attndate = t2.attnDate AND t1.TraineeID = t4.InstructorID
AND t1.attnDate='2016-01-13'
AND(CASE WHEN t4.Slot ='1' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '1') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '1')
WHEN t4.Slot ='2' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '2') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '2')
WHEN t4.Slot ='3' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '3') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '3')
WHEN t4.Slot ='4' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '4') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '4')
END)
GROUP BY t1.TraineeID, t2.attnDate

спросил(а) 2016-01-13T13:03:00+03:00 5 лет назад
1
Решение
140

Просто добавьте скобки к этим утверждениям союза. Он работает для меня, например: (выберите... limit 1) union (выберите... limit 1) union (выберите... limit 1)

союз со скобками тоже.

ответил(а) 2017-01-15T09:02:00+03:00 4 года назад
42

Это вызвано изменением в MySQL 5.7. Чтобы решить эту проблему, вы должны "вложить" свои SELECT, как показано ниже:

SELECT * FROM(

SELECT t1.TraineeID,
t2.attnDate,
MIN(t1.attnTime) AS inTime,
(CASE WHEN MIN(t1.attnTime) < MAX(t1.attnTime) THEN MAX(t1.attnTime) else '' end) AS outTime,
(CASE WHEN t5.Slot ='1' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '1')THEN 'P'
WHEN t5.Slot ='2' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '2')THEN 'P'
WHEN t5.Slot ='3' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '3')THEN 'P'
WHEN t5.Slot ='4' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '4')THEN 'P'
WHEN t5.Slot ='1' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '1')THEN 'Early Out'
WHEN t5.Slot ='2' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '2')THEN 'Early Out'
WHEN t5.Slot ='3' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '3')THEN 'Early Out'
WHEN t5.Slot ='4' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '4')THEN 'Early Out'
ELSE 'Late-In'
END) AS Status

FROM tbl_attn_temp t1, tbl_assigned t3, tbl_batch t5
JOIN (SELECT DISTINCT attnDate FROM tbl_attn_temp) t2
WHERE t1.attndate = t2.attnDate and t3.BatchID=t5.BatchID AND t1.TraineeID = t3.TraineeID
AND t1.attnDate='2016-01-13'
AND(CASE WHEN t5.Slot ='1' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '1') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '1')
WHEN t5.Slot ='2' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '2') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '2')
WHEN t5.Slot ='3' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '3') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '3')
WHEN t5.Slot ='4' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '4') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '4')
END)
GROUP BY t1.TraineeID, t2.attnDate
) AS query1

UNION

SELECT * FROM(
SELECT t1.TraineeID,
t2.attnDate,
MIN(t1.attnTime) AS inTime,
(CASE WHEN MIN(t1.attnTime) < MAX(t1.attnTime) THEN MAX(t1.attnTime) else '' end) AS outTime,
(CASE WHEN t4.Slot ='1' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '1')THEN 'P'
WHEN t4.Slot ='2' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '2')THEN 'P'
WHEN t4.Slot ='3' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '3')THEN 'P'
WHEN t4.Slot ='4' AND t1.attnTime<=(SELECT inTime FROM tbl_threshold WHERE id = '4')THEN 'P'
WHEN t4.Slot ='1' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '1')THEN 'Early Out'
WHEN t4.Slot ='2' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '2')THEN 'Early Out'
WHEN t4.Slot ='3' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '3')THEN 'Early Out'
WHEN t4.Slot ='4' AND max(t1.attnTime)<(SELECT outTime FROM tbl_threshold WHERE id = '4')THEN 'Early Out'
ELSE 'Late-In'
END) AS Status

FROM tbl_attn_temp t1, tbl_assigned t3, tbl_instructor_info t4
JOIN (SELECT DISTINCT attnDate FROM tbl_attn_temp) t2
WHERE t1.attndate = t2.attnDate AND t1.TraineeID = t4.InstructorID
AND t1.attnDate='2016-01-13'
AND(CASE WHEN t4.Slot ='1' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '1') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '1')
WHEN t4.Slot ='2' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '2') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '2')
WHEN t4.Slot ='3' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '3') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '3')
WHEN t4.Slot ='4' THEN t1.attnTime BETWEEN(SELECT minLimit FROM tbl_threshold WHERE id = '4') AND (SELECT maxLimit FROM tbl_threshold WHERE id = '4')
END)
GROUP BY t1.TraineeID, t2.attnDate
) as query2

ответил(а) 2017-11-21T22:37:00+03:00 3 года, 2 месяца назад
-6

попробуйте ключевое слово union all вместо union

пытаться:

select * from (query 1)
union all
(query 2)

ответил(а) 2016-01-13T13:20:00+03:00 5 лет назад
Ваш ответ
Введите минимум 50 символов
Чтобы , пожалуйста,
Выберите тему жалобы:

Другая проблема