SQL:如何从表`property`返回两个以上的行数据?

时间:2011-11-12 19:44:46

标签: mysql sql select join

  

可能重复:
  SQL - how to SELECT multiple tables and JOIN multiple rows from the same column?

我有三个MyQSL表 - languagecategoryproperty

/*
table `language`
`WHERE language.url='link'`*/
id   | url
-----+-----
111  | link

/*
table `category`
`SELECT category.id, category.order`*/
id   | order  | group | type | location
-----+--------+-------+------+---------
111  | 3      | a42   | a81  | a63

/*
table `property`
`LEFT JOIN properties ON properties.id = category.group`*/
id  | status
----+-------
a42 | public
a81 | update
a63 | states

和SQL

SELECT category.id, category.order, language.url, property.status AS `group`
FROM category
LEFT JOIN language
USING ( id )
LEFT JOIN property ON property.id = category.group
WHERE language.url='link'
LIMIT 1

返回

id  | order | url   | group
----+-------+-------+-------
111 | 3     | link  | public

SQL:如何从表property返回另外两行数据,以便结果为:

id  | order | url   | group  | type   | location
----+-------+-------+--------+--------+---------
111 | 3     | link  | public | update | states

1 个答案:

答案 0 :(得分:3)

您还需要2个LEFT JOIN s

SELECT category.id, category.order, language.url
   , g.status AS `group`, t.status AS `type`, l.status AS `location`
FROM category
LEFT JOIN language
USING ( id )
LEFT JOIN property AS g ON category.group = g.id
LEFT JOIN property AS t ON category.type = t.id 
LEFT JOIN property AS l ON category.location = l.id 
WHERE language.url='link'
LIMIT 1