需要一些帮助来理解为什么我的concat()失败以及如何解决。我从未使用过concat(),但是遇到了需要从另一个表中获取unit_nbr并将其连接到另一个字段以在主选择中创建一个字段的情况。
这是我使用的CONCAT():CONCAT(b.name, ' - ', unit_nbr) as lease_name
我正在寻找的输出类似于“ lease_name”的内容:John Doe-Unit 123
这是我的SQL:
SELECT a.lease_id, a.occupant_id, a.unit_id, (SELECT xx.unit_nbr FROM p_unit xx WHERE xx.unit_id = a.unit_id) as unit_nbr, c.name as prop_name, d.p_name, CONCAT(b.name, ' - ', unit_nbr) as lease_name
FROM o_leases a, p_occupants b, properties c, portfolio d
WHERE a.occupant_id = b.occupant_id
AND b.property_id = c.properties_id
AND c.portfolio_id = d.portfolio_id
AND a.archived = 1';
谁能帮我?谢谢。
最佳答案
您不使用这样的别名。
尝试:
SELECT a.lease_id, a.occupant_id, a.unit_id, xx.unit_nbr, c.name as prop_name, d.p_name, CONCAT(b.name, ' - ', xx.unit_nbr) as lease_name
FROM o_leases a, p_occupants b, properties c, portfolio d, p_unit xx
WHERE a.occupant_id = b.occupant_id
AND b.property_id = c.properties_id
AND c.portfolio_id = d.portfolio_id
AND a.archived = 1
AND xx.unit_id = a.unit_id;
关于sql - MYSQL CONCAT()失败,但我不知道为什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3408351/