我有两个看起来像这样的表
contacts
-------------------------------------------------------
| id | fname | lname | email | address | uid | uniqid |
-------------------------------------------------------
numbers
---------------------
| id | pnumber | cid |
---------------------
这是我的Sql代码
SELECT * FROM contacts LEFT JOIN numbers ON contacts.uniqid = numbers.cid
uniqid与cid相同,我的问题是当我尝试左连接这两个表时会返回此 注意:对不起,必须使用图片,因为当我尝试使用文字时,它会损坏
我想在同一列中获取所有pnumber,而无需重复。有什么办法可以做到这一点,还是我应该使用php?
预期结果
id, fname, lname, email, address, uid, uniqid, id, pnumber,pnumber,pnumber, cid
答案 0 :(得分:5)
将@Dao
public interface MyDao {
@Query("SELECT * FROM movie_table ORDER BY genre")
public ListMovies[] findMovieByGenre(String Genre);
}
与group_concat()
一起使用
distinct
答案 1 :(得分:2)
您可以使用GROUP BY语句和GROUP_CONCAT函数
SELECT
contacts.*
GROUP_CONCAT(numbers.pnumber)
FROM contacts
LEFT JOIN numbers ON contacts.uniqid = numbers.cid
GROUP BY contacts.id, contacts.fname, contacts.lname, contacts.email, contacts.address, contacts.uid, contacts.uniqid
答案 2 :(得分:2)
您可以使用DISTINCT过滤重复项:
SELECT DISTINCT pnumber FROM contacts LEFT JOIN numbers ON contacts.uniqid = numbers.cid
答案 3 :(得分:2)
尝试-
SELECT c.*, GROUP_CONCAT(n. pnumber SEPARATOR ', ') FROM contacts as c LEFT JOIN numbers as n ON c.uniqid = n.cid
GROUP BY c.id, c.fname, c.lname, c.email, c.address, c.uid, c.uniqid