我想选择列id_thread
,no_tlp
和nama
(联系人姓名)
但并非所有号码都保存在tb_contact
上(例如:来自sms-gateway的sms地址,它们具有自动名称值)
所以如果nama = NULL则设置' nama' =' no_tlp'
SELECT
id_thread, no_tlp,
IF(tb_contact.`nama` = 'NULL' THEN SET nama = no_tlp) AS nama
FROM
tb_sms
LEFT JOIN
tb_contact ON tb_sms.`no_tlp` = tb_contact.`no_tlp`
GROUP BY
id_thread ASC
这是我的查询,但它没有用,
请有人帮助我
答案 0 :(得分:0)
试试这个;)
SELECT id_thread, tb_contact.`no_tlp`, IF(tb_contact.`nama` IS NULL, tb_contact.`no_tlp`, tb_contact.`nama`) AS nama
FROM tb_sms
LEFT JOIN tb_contact
ON tb_sms.`no_tlp` = tb_contact.`no_tlp`
GROUP BY id_thread ASC
或使用COALESCE
之类的:
SELECT id_thread, tb_contact.`no_tlp`, COALESCE(tb_contact.`nama`, tb_contact.`no_tlp`) AS nama
FROM tb_sms
LEFT JOIN tb_contact
ON tb_sms.`no_tlp` = tb_contact.`no_tlp`
GROUP BY id_thread ASC
答案 1 :(得分:0)
请尝试此sql查询。
SELECT id_thread, no_tlp, CASE WHEN tb_contact.`nama` = 'NULL' THEN no_tlp END AS nama
FROM tb_sms
LEFT JOIN tb_contact
ON tb_sms.`no_tlp` = tb_contact.`no_tlp`
GROUP BY id_thread ASC