使用 MySQL数据库表上的此 SQL 查询,我已经为当前日期和客户选择了应用程序Web上的首次访问:< / p>
mysql> SELECT
(@cnt := @cnt + 1) AS rowNumber,
t.myDate,
t.myCustomer
FROM
tbl_new AS t
CROSS JOIN (SELECT @cnt := 0) AS dummy
WHERE
t.myDate IN (CURDATE())
ORDER BY
t.myDate DESC
LIMIT 1;
+-----------+------------+------------+
| rowNumber | myDate | myCustomer |
+-----------+------------+------------+
| 1 | 2018-11-12 | 561731A |
+-----------+------------+------------+
1 row in set
使用 MySQL数据库表上的 SQL 查询,我在网页上选择了当前日期和客户的最后访问权限:>
mysql> SELECT
(@cnt := @cnt + 1) AS rowNumber,
myDate,
myCustomer
FROM
tbl_new
ORDER BY
myDate DESC
LIMIT 1;
+-----------+------------+------------+
| rowNumber | myDate | myCustomer |
+-----------+------------+------------+
| 2 | 2018-11-12 | 719020A |
+-----------+------------+------------+
1 row in set
现在,我需要 UNION ALL 这些SQL查询才能获得唯一的输出,但是最后一次访问的输出是错误的908324A,而不是719020A,
为什么?
mysql> SELECT
(@cnt := @cnt + 1) AS rowNumber,
t.myDate,
t.myCustomer
FROM
tbl_new AS t
CROSS JOIN (SELECT @cnt := 0) AS dummy
WHERE
t.myDate IN (CURDATE())
UNION ALL
SELECT
(@cnt := @cnt + 1) AS rowNumber,
myDate,
myCustomer
FROM
tbl_new
ORDER BY
myDate DESC
LIMIT 2;
+-----------+------------+------------+
| rowNumber | myData | myCustomer |
+-----------+------------+------------+
| 246616 | 2018-11-12 | 561731A |
| 111872 | 2018-11-12 | 908324A |
+-----------+------------+------------+
2 rows in set
答案 0 :(得分:0)
我想说你的sql应该是这样的:
SELECT
(@cnt := @cnt + 1) AS rowNumber,
t.myDate,
t.myCustomer
FROM
tbl_new AS t
CROSS JOIN (SELECT @cnt := 0) AS dummy
WHERE
t.myDate IN (CURDATE())
ORDER BY
t.myDate DESC
LIMIT 1
UNION ALL
SELECT
(@cnt := @cnt + 1) AS rowNumber,
myDate,
myCustomer
FROM
tbl_new
ORDER BY
myDate DESC
LIMIT 1
使用UNON ALL只需两个句子。
已编辑
我无法发表评论。 也许您可以尝试将查询放在括号中:
SELECT * FROM (
SELECT (@cnt := @cnt + 1) AS rowNumber,
...
ORDER BY ...
)
UNION ALL
SELECT * FROM ( ... )
不确定,是否可以提供小提琴...