你可能想要类似的东西distinct
:
$sql = "SELECT distinct op.provider, g . * , gp . *, CONCAT(g.firstname, ' ', g.lastname) AS fullname
FROM openid AS op
INNER JOIN users AS g ON g.userid = op.userid
INNER JOIN profiles AS gp ON gp.userid = op.userid
WHERE op.openid =$openid";
或者您将 agroup by
与您希望将数据分组的列一起使用。
最后,如果您想将多行数据返回到单个字段(但它们不同),您可以使用 mysqlgroup_concat()
函数来执行此操作:
mysql> select * from first;
+------+-------+
| id | title |
+------+-------+
| 1 | aaaa |
| 2 | bbbb |
| 3 | cccc |
+------+-------+
3 rows in set (0.00 sec)
mysql> select group_concat(id) as IDs, group_concat(title) as Titles from first;
+-------+----------------+
| IDs | Titles |
+-------+----------------+
| 1,2,3 | aaaa,bbbb,cccc |
+-------+----------------+
1 row in set (0.00 sec)
好的,我在示例表中添加了一些额外的行,如下所示:
mysql> select * from first;
+------+-------+
| id | title |
+------+-------+
| 1 | aaaa |
| 2 | bbbb |
| 3 | cccc |
| 4 | NULL |
| 5 | eeee |
+------+-------+
5 rows in set (0.00 sec)
现在 agroup_concat
返回:
mysql> select group_concat(id) as IDs, group_concat(title) as Titles from first;
+-----------+---------------------+
| IDs | Titles |
+-----------+---------------------+
| 1,2,3,4,5 | aaaa,bbbb,cccc,eeee |
+-----------+---------------------+
1 row in set (0.00 sec)
但是您可以coalesce()
像这样使用该函数添加一个不错的占位符:
mysql> select group_concat(id) as IDs, group_concat(coalesce(title,'NoValueSpecial')) as Titles from first;
+-----------+------------------------------------+
| IDs | Titles |
+-----------+------------------------------------+
| 1,2,3,4,5 | aaaa,bbbb,cccc,NoValueSpecial,eeee |
+-----------+------------------------------------+
1 row in set (0.01 sec)
该coalesce()
函数查看多个列或您像我一样手动输入的值,并返回一个很好的标识符来发现您丢失的字段。它将从左到右评估空值。