Mysql 将多个元组选择为一个元组
Tis查询返回如下内容:Mysql 将多个元组选择为一个元组,mysql,sql,select,Mysql,Sql,Select,Tis查询返回如下内容: SELECT contact_id, name, email, phone, city FROM ak_contact WHERE email = 'test@gmail.com' ORDER BY contact_id DESC contact_id name email phone city 8499 Serj test@gmail.com 8498 Serj test@gmail.com
SELECT contact_id, name, email, phone, city
FROM ak_contact
WHERE email = 'test@gmail.com'
ORDER BY contact_id DESC
contact_id name email phone city
8499 Serj test@gmail.com
8498 Serj test@gmail.com 3-33-333
8494 Serj test@gmail.com London
8493 Serj test@gmail.com 2-22-222
但是我需要一个仅包含最新值的元组,如果它们不是null/空的话
所以我需要的结果应该是这样的:
SELECT contact_id, name, email, phone, city
FROM ak_contact
WHERE email = 'test@gmail.com'
ORDER BY contact_id DESC
contact_id name email phone city
8499 Serj test@gmail.com
8498 Serj test@gmail.com 3-33-333
8494 Serj test@gmail.com London
8493 Serj test@gmail.com 2-22-222
我通常不喜欢嵌套的select语句,但这是在MySQL中实现这一点的一种方法:
contact_id name email phone city
8499 Serj test@gmail.com 3-33-333 London
每个列可能来自不同的行,因此每个列都有自己的查询。更多关于Gordon答案的内容
select (select contact_id
from ak_contact
where email = 'test@gmail.com' and
contact_id is not null
order by contact_id desc
limit 1
) as contact_id,
(select name
from ak_contact
where email = 'test@gmail.com' and
name is not null
order by contact_id desc
limit 1
) as name,
(select phone
from ak_contact
where email = 'test@gmail.com' and
phone is not null
order by contact_id desc
limit 1
) as phone,
(select city
from ak_contact
where email = 'test@gmail.com' and
city is not null
order by contact_id desc
limit 1
) as city
结果
那么你所说的最新消息是什么意思?你怎么知道这张唱片是团体中最新的?你能详细说明你所说的最新唱片是什么意思吗?或者甚至扩展你的数据集,展示完整的表格结构。伙计们,如果你不能理解这个问题,这不是投票反对的理由。这不是我的错,数据库有愚蠢的结构!谢谢这是可行的,但在这种情况下看起来非常可怕,因为php上的foreach会更好。我认为OP需要最新的电子邮件too@hop根据问题中的示例,他们正在对电子邮件进行硬编码。谢谢您的回答。否,此键中的电子邮件是表的非正式键。有人愚蠢地创建了此表:@ypercube似乎缺少电子邮件。这是在添加一行: