如何搜索两个表有不同列的表

时间:2016-11-21 06:44:19

标签: php codeigniter

有什么方法可以搜索表的数据,我将使用两个标准Firstname和lastname?

表1

Firstname | LastName | Gender

| Donald | Trump | Male|

表2

GFirstName | GLastName | Gender

|Hillary| Clinton | Female

|Donald | McDonald| Female

就我而言,我想搜索"唐纳德特朗普"

和MySQL数据应该显示

的信息

表1

 Donald | Trump| Male

我尝试使用此查询,但它没有给我任何东西

SELECT *
FROM table1
WHERE 'Firstname' LIKE %Donald Trump%
  OR 'LastName' LIKE %Donald Trump%
  UNION ALL
  SELECT *
  FROM table2 WHERE 'Firstname' LIKE %Donald Trump%
  OR 'LastName' LIKE %Donald Trump%

2 个答案:

答案 0 :(得分:1)

更改如下所示的查询,将括号括起来或条件

SELECT *
FROM table1
WHERE ('Firstname' LIKE %Donald Trump%
  OR 'LastName' LIKE %Donald Trump%)
  UNION ALL
  SELECT *
  FROM table2 WHERE ('Firstname' LIKE %Donald Trump%
  OR 'LastName' LIKE %Donald Trump%)

答案 1 :(得分:1)

尝试

SELECT *
  FROM table1
  WHERE 
         CONCAT (Firstname, ' ',LastName)  LIKE '%Donald Trump%' 
  UNION ALL
  SELECT *
  FROM table2 WHERE CONCAT (Firstname, ' ',LastName)  LIKE '%Donald Trump%'