简体   繁体   English

连接同一表的多行

[英]Join multiple rows of same tables

I am trying to pull 2 different sets of data off of 1 table. 我正在尝试从1个表中提取2组不同的数据。 I am not quite sure how to approach this though. 我不太确定如何解决这个问题。 This is the current table set up (that pertinent). 这是当前表的设置(相关)。

+----+-----------+----------+------+------------+--------+--------+
| id | recipient | given_by | time | expiration | points | reason |
+----+-----------+----------+------+------------+--------+--------+
|  1 |   72      |     1    | time |    time    |   2    |  test  |
|  3 |   15      |     4    | time |    time    |   5    |  test  |
+----+-----------+----------+------+------------+--------+--------+

+----+----------+
| id | username |
+----+----------+
| 1  |   admin  |
| 4  |   user4  |
...
| 15 |  user15  |
...
| 72 |  user72  |
+----+----------+

I was able to get the recipient to match up to a name by using this query: 通过使用以下查询,我能够使收件人匹配姓名:

SELECT 
   usr.username, usr.id,  sl.recipient, sl.given_by, 
   sl.time, sl.experation, sl.points, sl.reason
FROM 
    DD_users AS usr
LEFT JOIN
    DD_schittlist AS sl
ON (sl.recipient = usr.id)       
GROUP BY
        usr.id
ORDER BY 
        sl.points DESC,
        usr.username

That will match up recipient 72 to user72 but I also want to make given by 1 to show admin and given_by 4 to show as user4. 那将使接收者72与user72匹配,但我还想使1给定显示admin,give_by 4显示为user4。

Try this: (All you need to do is alias a 2nd reference to the DD_users table and add a 2nd join statement, then reference the aliased username column in your select statement.) 尝试以下操作:(您需要做的就是为DD_users表的第二个引用添加别名,并添加第二个join语句,然后在您的select语句中引用别名的username列。)

  SELECT 
    usr.username, usr.id,  sl.recipient, sl.given_by, usr2.username, sl.time, sl.experation, sl.points, sl.reason
  FROM 
    DD_users AS usr
  LEFT JOIN DD_schittlist AS sl  ON (sl.recipient = usr.id)    
  JOIN DD_users as usr2 ON (usr2.id = sl.given_by)   
  GROUP BY
    usr.id
  ORDER BY 
    sl.points DESC,
    usr.username

You need to join twice against the DD_users table, once for id as you already have, and another for given_by . 您需要针对DD_users表连接两次,一次连接已有的id ,另一次连接给given_by Each gets its own alias, which must be used in the SELECT list to disambiguate them ( usr, given ) 每个都有自己的别名,必须在SELECT列表中使用它们来消除歧义( usr, given

  SELECT 
    /* The original username you already had */
    usr.username, 
    usr.id,  
    sl.recipient, 
    sl.given_by, 
    /* The given_by username from the given alias table */
    /* Be sure to alias this column here as well to differentiate them in the client */
    given.username AS given_by_username,
    sl.time, 
    sl.experation, 
    sl.points, 
    sl.reason
  FROM 
    DD_users AS usr
    /* First join you already had between id and recipient */
    LEFT JOIN DD_schittlist AS sl ON (sl.recipient = usr.id)       
    /* A second LEFT JOIN between DD_shittlist and DD_users as given */
    /* (LEFT JOIN permits nulls in DD_shittlist.given_by) */
    LEFT JOIN DD_users AS given ON sl.given_by = given.id
  GROUP BY
    usr.id
  ORDER BY 
    sl.points DESC,
    usr.username

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

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