你好,可以帮我吗?我试图合并两个除了某些字段之外根本没有关系的表,但主要思想是:
- 合并两个表
- 查找其字段中包含“代码”的项目。 但总是有错误说
#1054 - Unknown column 'username' in 'where clause'
SELECT role ,id,'' as type, '' as logo, username,'' as contactperson, lastname, '' as companyname, firstname, '' as address, description, address FROM users
UNION
SELECT '' as role, id,type, logo,'' as username, contactperson, '' as lastname, companyname, '' as firstname, address, description, website FROM company
WHERE type LIKE '%code%'
OR logo LIKE '%code%'
OR username LIKE '%code%'
OR contactperson LIKE '%code%'
OR lastname LIKE '%code%'
OR companyname LIKE '%code%'
OR firstname LIKE '%code%'
OR address LIKE '%code%'
OR description LIKE '%code%'
OR address LIKE '%code%')
我知道我的代码似乎太过分了,请帮帮我吗?或帖子与我的问题相同?
最佳答案
根据表的结构,使用union
并不是一个好主意。最好使用基于这两个表之间的少数匹配列的联接并创建新表并在新表上执行搜索。例如link 。
要回答您的问题,请在用户名
前添加用户
。
示例:
users.username LIKE '%code%'
关于mysql - 通过组合两个表格与特定关键字进行比较来创建搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19534903/