Mysql多表连接派生表

时间:2015-05-11 16:14:55

标签: mysql self-join

My Table Details image with test data

我提出了以下代码:

select tableName,userDets,regDate
from (
(SELECT 'Table1' AS tableName, t1.username as userDets, t1.created_date as regDate FROM table1 t1 group by username )
union all
(SELECT 'Table2' AS tableName, t2.username as userDets, t2.created_date as regDate FROM table2 t2 group by username )
union all 
(SELECT 'Table3' AS tableName, t3.username as userDets, t3.created_date as regDate FROM table3 t3 group by username )
) AS allTableCombo 
group by allTableCombo.userDets

如果在此代码中我最后按日期使用顺序,然后使用group by username将其子查询为另一个select,我可以获得所需的输出。但我不想使用sbuquery 如果有人能为我提供另一种选择..我尝试过自我加入,但在使用它时没有成功....

0 个答案:

没有答案