简体   繁体   English

如何在表中组合两列的两个值。

[英]How to combine two value of two column in a table.

Is there any way to select data from table like below? 有没有办法从下表中选择数据?

SELECT * FROM profile WHERE fname+" "+lname LIKE '%$name%'

For example: 例如:

fname = "Lady"
lname = "Gaga"
$name = "lady g" is match

Just like the facebook search engine, when I input a full name, it will display the result even the full name included non breaking space. 就像facebook搜索引擎一样,当我输入一个全名时,它会显示结果,甚至包括非破坏空间的全名。

There sure is - you can use the mysql CONCAT() function - 肯定的是 - 你可以使用mysql CONCAT()函数 -

SELECT * FROM profile WHERE CONCAT(fname," ",lname) LIKE '%$name%'

The CONCAT() function can take many arguments, quoted strings are taken as their literal values and field names evaluate to their value from the table being queried. CONCAT()函数可以使用许多参数,引用的字符串作为其文字值,字段名称从被查询的表中计算其值。 So fname and lname must be the actual names of the fields in your database. 因此fnamelname必须是数据库中字段的实际名称。

See here for some more in-depth documentation 请参阅此处以获取更深入的文档

是的,使用CONCAT()函数,如下所示:

SELECT * FROM profile WHERE CONCAT(fname,' ',lname) LIKE '%$name%'

In MySQL, you need to use CONCAT() or CONCAT_WS() to concatenate two objects. 在MySQL中,您需要使用CONCAT()CONCAT_WS()来连接两个对象。

SELECT * FROM profile WHERE CONCAT(fname, ' ', lname) LIKE '%$name%'

OR 要么

SELECT * FROM profile WHERE CONCAT_WS(' ', fname, lname) LIKE '%$name%'

You would use the CONCAT function. 您将使用CONCAT功能。 Keep in mind this is likely to have poor performance as it wont use indexes correctly. 请记住,这可能会导致性能不佳,因为它不会正确使用索引。

SELECT * FROM profile WHERE CONCAT(fname," ",lname) LIKE '%$name%'

You can use CONCAT_WS : 您可以使用CONCAT_WS

SELECT * FROM profile WHERE CONCAT_WS(' ', fname, lname) LIKE '%' . $name . '%'

Remembering to escape the input, of course (or to use prepared statements ). 记住要记住逃避输入(或使用预备语句 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM