1

我有 2 个表,其中table1包括用户名和table2用户x做某事的日期。

 table1:               table2:        

|----------|          |-------|----------|
| username |          | User  | date     |
|----------|          |-------|----------|
| user1    |          | user1 | 1.1.2013 |
| user2    |          | user2 | 2.4.2013 |
| user3    |          |-------|----------|
|----------|     

现在我想得到一个列表,显示所有用户table1的行数table2,还显示日期是否适用于所有或 2 个或仅一个用户。

|-------|----------|
| User1 | 1.1.2013 |
| user2 | -        |
| user3 | -        |
| user1 | -        |
| user2 | 2.3.2013 |
| user3 | -        |
|-------|----------|

所以基本上我只想在从 table2 读取行时,其他用户在同一日期做了什么。

如果您有任何想法如何做到这一点,请帮助我:)


我需要添加的下一件事是几个月

| 月/年年 | 用户 | 公斤 |
|---------|--------|------|
| 01/2013 | 用户1 | 0 |
| 01/2013 | 用户2 | 0 |
| 01/2013 | 用户3 | 5 |
| 02/2013 | 用户1 | 2 |
| 02/2013 | 用户2 | 4 |
| 02/2013 | 用户3 | 7 |
| ... | ... | ... |
| 12/2013 | 用户1 | 0 |
| 12/2013 | 用户2 | 1 |
| 12/2013 | 用户3 | 0 |
|---------|--------|------|

我认为对于这个表,我需要在最新更新的查询中使用某种 if 语句。而且我不介意是否有解决方案可以分享,因为我认为我无法很快解决这个问题:)

所以我需要检查一下,是否有人在一月份钓到了鱼,如果没有 => 标记 0 如果您有几条鱼,请标记平均值或该月的总和给有几条鱼的用户。与之前的想法相同,但这允许更好的折线图“段”,因为所有测量点都在同一条垂直线上。

4

4 回答 4

0
SELECT username, date FROM table1 t1 
FULL OUTER JOIN table2 t2 
ON t1.username = t2.user
于 2013-11-03T22:11:56.113 回答
0

我喜欢这个任务。不要认为这是最佳解决方案,但效果很好:

SELECT un, tmp 
FROM (
    SELECT * 
    FROM (
        SELECT  t1.username as un, 
            t2.user as u, 
            t2.date as d, 
            t2.date as tmp
        FROM table1 as t1, table2 as t2 
        WHERE t1.username = t2.user
        UNION
        SELECT  
            t1.username as un, 
            t2.user as u, 
            t2.date as d, 
            '-' as tmp
        FROM table1 as t1, table2 as t2 
        WHERE t1.username <> t2.user
    ) AS res
    ORDER BY d ASC
) AS tot_res

更新:

SELECT un, tmp 
FROM (
    SELECT * 
    FROM (
        SELECT  t1.username as un, 
            t2.user as u, 
            t2.date as d, 
            t2.date as tmp
        FROM table1 as t1, table2 as t2 
        WHERE t1.username = t2.user
        UNION
        SELECT  
            t1.username as un, 
            t2.user as u, 
            t2.date as d, 
            '-' as tmp
        FROM table1 as t1, table2 as t2 
        WHERE t1.username <> t2.user    
    ) AS res
    GROUP BY res.un, res.d
    ORDER BY d ASC
) AS tot_res
于 2013-11-05T00:45:02.497 回答
0
|-------|----------|
| User1 | 1.1.2013 |
| user2 | -        |
| user3 | -        |<-- we need some way of distinguishing between this row...
| user1 | -        |
| user2 | 2.3.2013 |
| user3 | -        |<-- and this row
|-------|----------|

所以

SELECT DISTINCT x.*
              , y.date
              , CASE WHEN z.date IS NULL THEN 0 ELSE 1 END catch 
           FROM table1 x 
           JOIN table2 y 
           LEFT 
           JOIN table2 z 
             ON z.user = x.username 
            AND z.date = y.date
          ORDER
             BY x.username
              , y.date;

sqlfiddle.com/#!2/81494/6

(并且,显然,为了使其正常工作,需要使用日期数据类型存储日期)

于 2013-11-06T16:51:24.637 回答
0
SELECT username,date FROM table1 t1 
JOIN table2 t2 ON t1.username=t2.user GROUP BY date ORDER BY date
于 2013-11-03T21:58:59.667 回答