-1

我有一个大表,现在表按日期细分为几个

例如

原表

UserLoginTable
+---------+------------+---------+
| UserID  | LoginTime  | others  |
+---------+------------+---------+
|  1      | 2013-03-12 | ....    |
+---------+------------+---------+
|  2      | 2013-05-12 | ....    |
+---------+------------+---------+
|  1      | 2013-06-12 | ....    |
+---------+------------+---------+
|  ...    | ...        | ....    |   
+---------+------------+---------+

现在表格是:

UserLoginTable_Date(yyyyMM)

用户登录表_201303

+---------+------------+---------+
| UserID  | LoginTime  | others  |
+---------+------------+---------+
|  1      | 2013-03-12 | ....    |
+---------+------------+---------+

用户登录表_201304

+---------+------------+---------+
| UserID  | LoginTime  | others  |  //this table is not have UserID=1
+---------+------------+---------+
|  2      | 2013-04-01 | ....    |
+---------+------------+---------+

我用了

select count(*) from UserLoginTable_201307 where UserID=1
select count(*) from UserLoginTable_201306 where UserID=1
...

所以我想知道如何通过一个 sql 显示这种格式

+--------------+ 
| UserInYear   |  
+--------------+ 
|  201303      |  //here dose find User in 201304
+--------------+
|  201306      |  
+--------------+
|  ....        |  
+--------------+

最佳效率点。谢谢。

4

3 回答 3

0

用于UNION ALL

SELECT COUNT(*) AS UserInYear FROM UserLoginTable_201307 WHERE UserID = 1
UNION ALL
SELECT COUNT(*) FROM UserLoginTable_201306 WHERE UserID = 1

如果任何年份的 UserInYear 存在重复值,则将UNION ALL全部显示,而UNION不会显示。所以使用UNION ALL.

于 2013-07-24T07:25:50.703 回答
0

尝试union all如下使用

select count(*) as UserInYear from UserLoginTable_201307 where UserID=1
union all
select count(*) from UserLoginTable_201306 where UserID=1

注意union all不会消除重复。如果您不想重复,则必须使用unioninsetad。

于 2013-07-24T07:25:55.547 回答
0

子查询的一个例子:

SELECT column_name, column_name1, column_number
FROM their_table
WHERE column_number = (SELECT MAX(column_number) FROM the_other_table);

假设 column_name 作为 customerNumber,column_name1 作为 checkNumber,column_number 作为 amo

于 2014-03-13T21:58:24.047 回答