0

你好,你能帮帮我吗?我正在尝试合并两个除了某些字段之外根本没有关系的表,但主要思想是:

  • 合并两个表
  • 查找在其字段中包含“代码”的项目。但总是有错误说

#1054 - “where 子句”中的未知列“用户名”

 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%')

我知道我的代码似乎有点矫枉过正,请帮帮我?或与我的问题相同的帖子?

4

1 回答 1

0

根据表的结构使用union不是一个好主意。最好使用基于这两个表之间的少数匹配列的连接并创建新表并在新表上执行搜索。例如链接

username要回答您的问题,请以.为前缀users

例子:
users.username LIKE '%code%'

于 2013-10-23T07:31:05.750 回答