我在一个查询中执行多个选择时遇到问题。这就是我的工作:
SELECT 1 AS VisitsToday, `Datum Bezoek 1` FROM `psttodo-uit` WHERE CAST(`Datum Bezoek 1` AS DATE) = CURRENT_DATE AND PB = 1
UNION
SELECT 2 AS ConversationsToday, `Datum Bezoek 1` FROM `psttodo-uit` WHERE CAST(`Datum Bezoek 1` AS DATE) = CURRENT_DATE AND PG = 1
UNION
SELECT 3 AS PermitsToday, `Datum Bezoek 1` FROM `psttodo-uit` WHERE CAST(`Datum Bezoek 1` AS DATE) = CURRENT_DATE AND PA = 1;
但我得到了这个结果:
array (size=15)
0 =>
array (size=2)
'VisitsToday' => string '1' (length=1)
'Datum Bezoek 1' => string '2014-02-28 08:25:34' (length=19)
1 =>
array (size=2)
'VisitsToday' => string '1' (length=1)
'Datum Bezoek 1' => string '2014-02-28 08:55:34' (length=19)
2 =>
array (size=2)
'VisitsToday' => string '1' (length=1)
'Datum Bezoek 1' => string '2014-02-28 12:22:34' (length=19)
3 =>
array (size=2)
'VisitsToday' => string '1' (length=1)
'Datum Bezoek 1' => string '2014-02-28 12:28:34' (length=19)
4 =>
array (size=2)
'VisitsToday' => string '1' (length=1)
'Datum Bezoek 1' => string '2014-02-28 16:13:34' (length=19)
5 =>
array (size=2)
'VisitsToday' => string '2' (length=1)
'Datum Bezoek 1' => string '2014-02-28 08:25:34' (length=19)
6 =>
array (size=2)
'VisitsToday' => string '2' (length=1)
'Datum Bezoek 1' => string '2014-02-28 08:55:34' (length=19)
7 =>
array (size=2)
'VisitsToday' => string '2' (length=1)
'Datum Bezoek 1' => string '2014-02-28 12:22:34' (length=19)
8 =>
array (size=2)
'VisitsToday' => string '2' (length=1)
'Datum Bezoek 1' => string '2014-02-28 12:28:34' (length=19)
9 =>
array (size=2)
'VisitsToday' => string '2' (length=1)
'Datum Bezoek 1' => string '2014-02-28 16:13:34' (length=19)
10 =>
array (size=2)
'VisitsToday' => string '3' (length=1)
'Datum Bezoek 1' => string '2014-02-28 08:25:34' (length=19)
11 =>
array (size=2)
'VisitsToday' => string '3' (length=1)
'Datum Bezoek 1' => string '2014-02-28 08:55:34' (length=19)
12 =>
array (size=2)
'VisitsToday' => string '3' (length=1)
'Datum Bezoek 1' => string '2014-02-28 12:22:34' (length=19)
13 =>
array (size=2)
'VisitsToday' => string '3' (length=1)
'Datum Bezoek 1' => string '2014-02-28 12:28:34' (length=19)
14 =>
array (size=2)
'VisitsToday' => string '3' (length=1)
'Datum Bezoek 1' => string '2014-02-28 16:13:34' (length=19)
我也尝试了以下内容:
SELECT `Datum Bezoek 1` AS VisitsToday FROM `psttodo-uit` WHERE CAST(`Datum Bezoek 1` AS DATE) = CURRENT_DATE AND PB = 1
UNION
SELECT `Datum Bezoek 1` AS ConversationsToday FROM `psttodo-uit` WHERE CAST(`Datum Bezoek 1` AS DATE) = CURRENT_DATE AND PG = 1
UNION
SELECT `Datum Bezoek 1` AS PermitsToday FROM `psttodo-uit` WHERE CAST(`Datum Bezoek 1` AS DATE) = CURRENT_DATE AND PA = 1
但后来我得到了这个结果:
array (size=5)
0 =>
array (size=1)
'VisitsToday' => string '2014-02-28 08:25:34' (length=19)
1 =>
array (size=1)
'VisitsToday' => string '2014-02-28 08:55:34' (length=19)
2 =>
array (size=1)
'VisitsToday' => string '2014-02-28 12:22:34' (length=19)
3 =>
array (size=1)
'VisitsToday' => string '2014-02-28 12:28:34' (length=19)
4 =>
array (size=1)
'VisitsToday' => string '2014-02-28 16:13:34' (length=19)
我试图得到的是一个包含3个键“VisitsToday”,“ConversationsToday”和“PermitsToday”的数组,每个键都包含一个从数据库中选择日期的数组。
更新
使用不同列的解决方法如下:
SELECT 1 AS VisitsToday, NULL AS ConversationsToday, NULL AS PermitsToday
FROM `psttodo-uit` WHERE CAST(`Datum Bezoek 1` AS DATE) = CURRENT_DATE AND PB = 1)
UNION
SELECT NULL, 2 AS ConversationsToday, NULL
FROM `psttodo-uit` WHERE CAST(`Datum Bezoek 1` AS DATE) = CURRENT_DATE AND PG = 1)
UNION
SELECT NULL, NULL, 3 AS PermitsToday
FROM `psttodo-uit` WHERE CAST(`Datum Bezoek 1` AS DATE) = CURRENT_DATE AND PA = 1)
但后来我收到这样的错误:
Syntax error or access violation: 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 NULL, 2 AS ConversationsToday, NU' at line 2
答案 0 :(得分:2)
您试图为一列提供多个名称 - 这根本无法完成。使用union时,将在第一个选择中分配列名
More info on UNION
可能的解决方法是添加不同的列,因此它看起来像这样:
SELECT 1 AS VisitsToday, NULL AS ConversationsToday, NULL AS PermitsToday, ... FROM ... UNION SELECT NULL , 2 , NULL , ... FROM ... ... UNION SELECT NULL , NULL , 3 , ... FROM ... ..;