MySQLselectCONCAT条件

我试图在我的脑海里编译这个..我有一个名字和姓氏字段的表,我有一个像“鲍勃琼斯”或“鲍勃·迈克尔琼斯”和其他几个string。

事情是,我有例如鲍勃在名,迈克尔·琼斯在姓氏

所以我试图

SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast FROM users WHERE firstlast = "Bob Michael Jones" 

但它说未知列“firstlast”..任何人都可以帮忙吗?

您给出的别名是查询的输出 – 它们在查询本身中不可用。

您可以重复expression式:

 SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast FROM users WHERE CONCAT(firstname, ' ', lastname) = "Bob Michael Jones" 

或者包装查询

 SELECT * FROM ( SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast FROM users) base WHERE firstLast = "Bob Michael Jones" 

尝试这个:

 SELECT * FROM ( SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast FROM users ) a WHERE firstlast = "Bob Michael Jones" 
 SELECT needefield, CONCAT(firstname, ' ',lastname) as firstlast FROM users WHERE CONCAT(firstname, ' ', lastname) = "Bob Michael Jones" 

使用CONCAT_WS()。

 SELECT CONCAT_WS(' ',firstname,lastname) as firstlast FROM users WHERE firstlast = "Bob Michael Jones"; 

第一个参数是其余参数的分隔符。

尝试:

 SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast FROM users WHERE CONCAT(firstname, ' ', lastname) = "Bob Michael Jones" 

您的别名firstlast在查询的where子句中不可用,除非您将查询作为子select执行。

有重复CONCATexpression式或使用子查询的替代方法。 您可以使用HAVING子句来识别列别名。

 SELECT neededfield, CONCAT(firstname, ' ', lastname) AS firstlast FROM users HAVING firstlast = "Bob Michael Jones" 

这是一个工作的SQL小提琴 。

1.我有两个表table1table2 2.在table1中fieldname是cert_no ,在table2 fieldname是cer1,cert2,cert3,cert4,cert5 3.不在table2 (cer1,cert2,cert3,cert4,cert5)table2 (cer1,cert2,cert3,cert4,cert5)单独想要显示4.如果两个表具有相同的值只有transfile_file要显示

查询我放的是什么

 SELECT * FROM table1 WHERE folio = 'KCA00619' AND cm_flag !='X' AND certificate_no NOT IN (SELECT CONCAT(certno1,certno2,certno3,certno4,certno5,certno6,certno7,certno8,certno9,certno10) FROM table2 WHERE tofolio = '123456')