查询我正在使用
SELECT v.*, if( up.upload_key = 'send', up.upload_value, 0 ) AS send,
if( up.upload_key = 'host', up.upload_value, 0 ) AS host,
if( up.upload_key = 'upload_id', up.upload_value, 0 ) AS upload_id
FROM TableInit v JOIN TableName up ON up.video_id = v.id_video;
查询的返回结果是这个
| id_video | titulo | desc | send | host | upload_id |
|----------|--------|------|------|------|--------------|
| 6 | Title | Desc | 0 | cnn | 0 |
| 6 | Title | Desc | 0 | 0 | 0 |
| 6 | Title | Desc | 0 | 0 | sHGN-tSNvJYs |
我需要的退货是:
| id_video | titulo | desc | send | host | upload_id |
|----------|--------|------|------|------|-------------|
| 6 | Title | Desc | 0 | cnn |sHGN-tSNvJYs |
如果我使用GROUP BY,那么我得到的返回值只是第一行的值,而不是其他行的值
| id_video | titulo | desc | send | host | upload_id |
|----------|--------|------|------|------|-----------|
| 6 | Title | Desc | 0 | cnn | 0 |
答案 0 :(得分:1)
这应该可以完成工作:
SELECT v.*, if( up.upload_key = 'send', up.upload_value, 0 ) AS send,
MAX(if( up.upload_key = 'host', up.upload_value, null )) AS host,
MAX(if( up.upload_key = 'upload_id', up.upload_value, null )) AS upload_id
FROM TableInit v JOIN TableName up ON up.video_id = v.id_video GROUP BY id_video;
我刚刚添加了MAX
函数,以仅选择列host和upload_id的最大值。
返回的结果:
+----------+--------+------+------+------+--------------+
| id_video | titulo | desc | send | host | upload_id |
+----------+--------+------+------+------+--------------+
| 6 | Title | Desc | 0 | cnn | sHGN-tSNvJYs |
+----------+--------+------+------+------+--------------+