简体   繁体   English

LEFT JOIN仅显示第一张表的第一行

[英]LEFT JOIN show first row from first table only one time

I have two: 我有两个:

Firest table: table1 壁炉表:table1

id   |  text   
-------------
1       t1      
2       t2           
...

Second table: table2 第二张表:table2

id   |  idTable1 |  text2
-------------------------------
1       1           text1Table2
2       1           text2Table2
3       1           text3Table2
4       2           text4Table2
5       2           text5Table2
...

If i do LEFT JOIN: 如果我做左加入:

SELECT * FROM table1 t1 LEFT JOIN table2 t2 ON t1.id=t2.idTable1

the result is this: 结果是这样的:

id  |   text |  id | idTable1 | text2
------------------------------------------
1       t1      1   1           text1Table2
1       t1      2   1           text2Table2
1       t1      3   1           text3Table2
2       t2      4   2           text4Table2
2       t2      5   2           text5Table2
...

But i want the rows that are duplicated in the first table to be displayed only once like this: 但是我希望在第一个表中重复的行仅显示一次,如下所示:

id  |   text |  id | idTable1 | text2
------------------------------------------
1       t1      1   1           text1Table2
1       -       2   1           text2Table2
1       -       3   1           text3Table2
2       t2      4   2           text4Table2
2       -       5   2           text5Table2
...

Edit: And this result to add in mysql view 编辑:并将此结果添加到mysql视图中

You can do this using variables: 您可以使用变量来做到这一点:

SELECT t1.id,
       if(@prevtext = text, '-', if(@prevtext := text, t1.text, t1.text)) as text,
       t2.id, t2.idTable1, t2.text2
FROM table1 t1 LEFT JOIN
     table2 t2
     ON t1.id = t2.idTable1 CROSS JOIN
     (select @prevtext := '') vars
ORDER BY t1.id, t1.text;

However, I discourage you from doing this. 但是,我不鼓励您这样做。 A result set in SQL has the same properties as a table. SQL中的结果集具有与表相同的属性。 Namely, each row is independent of the other rows. 即,每一行独立于其他行。 When you put data in this format, you are requiring a particular ordering of the result set, so it no longer has the properties of a SQL table. 以这种格式放置数据时,需要对结果集进行特定的排序,因此它不再具有SQL表的属性。

EDIT: 编辑:

I thought this was impossible to do with a subquery compatible with a view, but it may be possible given the data your query (where the t2 id is ordered): 我认为这与与视图兼容的子查询是不可能的,但是鉴于您查询的数据(排序t2 id的地方),这可能是可行的:

SELECT t1.id,
       (case when text = (select text
                          from table2 tt2
                          where tt2.id < t2.id
                          order by tt2.id desc
                          limit 1
                         )
             then '-' else text
        end) as text
       t2.id, t2.idTable1, t2.text2
FROM table1 t1 LEFT JOIN
     table2 t2
     ON t1.id = t2.idTable1 
ORDER BY t1.id, t2.id;

You can use a case statement and a subquery to determine whether a row is the first (ie has the lowest table2.id ) for a given table2.idTable 您可以使用case语句和子查询来确定给定table2.idTable的行是否为第一行(即table2.id最低)。

CREATE VIEW myView AS SELECT 
    t1.id id1,
    (CASE
        WHEN t2.idTable1 IS NULL OR NOT EXISTS (
            SELECT 1 
            FROM table2 t3
            WHERE t3.idTable1 = t2.idTable1
            AND t3.id < t2.id
        ) THEN t1.text ELSE '-'
    END) text1,
    t2.id id2,    
    t2.idTable1,
    t2.text2
FROM table1 t1 
LEFT JOIN table2 t2 ON t1.id = t2.idTable1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM