おそらく次のようなものが必要です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";
それまたは、group by
データをグループ化する列で a を使用します。
最後に、複数行のデータを 1 つのフィールドに返したい場合 (ただしそれらは異なります)、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)
そして今、これをgroup_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()
関数は、複数の列または私が行ったように手動で入力した値のいずれかを調べ、欠落しているフィールドを見つけるための優れた識別子を返します。null 値は左から右に評価されます。