1

I found help at this topic: Search different column names in different tables in MySQL but they are only referencing two fields per table in that topic and I wasn't able to generate an answer from my searches.

EDIT : I have also found this thread: Combine two tables that have no common fields which gives a little more helpful information, but I am still not able to figure out how to combine these properly

I have never had the need to create a JOIN or UNION query, so this is all pretty new to me. So forgive me if there is an easy solution to this.

I need to run a query that will search through two different database tables that have completely different fields.

Here are the two queries I am trying to combine into one:

"SELECT * FROM seekers 
    WHERE (first_name LIKE '%".$q."%') 
        OR (last_name LIKE '%".$q."%') 
        OR (username LIKE '%".$q."%') 
        OR (school LIKE '%".$q."%') 
        OR (major LIKE '%".$q."%') 
        OR (employer LIKE '%".$q."%') 
        OR (position LIKE '%".$q."%') 
        OR (background LIKE '%".$q."%') 
        OR (interests LIKE '%".$q."%') 
        OR (skills LIKE '%".$q."%')
    ORDER BY id DESC"

"SELECT * FROM companies 
    WHERE (company_name LIKE '%".$q."%') 
        OR (username LIKE '%".$q."%') 
        OR (industry LIKE '%".$q."%') 
        OR (slogan LIKE '%".$q."%')
        OR (location LIKE '%".$q."%') 
    ORDER BY id"

After the query, I want to get all the fields into an array and display the data accordingly.

I guess the major problem I am having with this is deciding how I should go about combining the tables? Do I want to use a JOIN (inner or outer) or do I want to use a UNION?

I have tried to create a VIEW by doing this: $this->mysqli->query("CREATE VIEW userTable AS (SELECT * FROM seekers) UNION ALL (SELECT * FROM companies)"); but that didn't work, I'm guessing because the tables have far different fields.

EDIT : After trying some of the answers below, I have come up with this query:

SELECT first_name, last_name, username, school, major, employer, position, background, interests, skills 
    FROM seekers 
        WHERE (first_name LIKE '%".$q."%') 
        OR (last_name LIKE '%".$q."%') 
        OR (username LIKE '%".$q."%') 
        OR (school LIKE '%".$q."%') 
        OR (major LIKE '%".$q."%') 
        OR (employer LIKE '%".$q."%') 
        OR (position LIKE '%".$q."%') 
        OR (background LIKE '%".$q."%') 
        OR (interests LIKE '%".$q."%') 
        OR (skills LIKE '%".$q."%')
    UNION ALL 
        SELECT company_name, username, industry, slogan, location, NULL, NULL, NULL, NULL, NULL 
            WHERE (company_name LIKE '%".$q."%') 
                OR (username LIKE '%".$q."%') 
                OR (industry LIKE '%".$q."%') 
                OR (slogan LIKE '%".$q."%') 
                OR (location LIKE '%".$q."%') 

I removed the ORDER BY clause because I was getting an error saying "Incorrect use of UNION and ORDER BY", so after removing the ORDER BY I get this error: "Query Failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE (company_name LIKE '%0%') OR (username LIKE '%0%') OR (industry LIKE '%0%')' at line 1"

Why is $q becoming 0 in the query after the union?

EDIT #2 : Here is a JOIN query I put together that actually returns data, but it doesn't return the correct data. I don't know if this will help answer the question, but I thought I would put it here anyway.

"SELECT * FROM seekers t1 LEFT JOIN companies t2 ON t1.id = t2.id WHERE (t1.first_name LIKE '%".$q."%') OR (t1.last_name LIKE '%".$q."%') OR (t1.username LIKE '%".$q."%') OR (t1.school LIKE '%".$q."%') OR (t1.major LIKE '%".$q."%') OR (t1.employer LIKE '%".$q."%') OR (t1.position LIKE '%".$q."%') OR (t1.background LIKE '%".$q."%') OR (t1.interests LIKE '%".$q."%') OR (t1.skills LIKE '%".$q."%') OR (t2.company_name LIKE '%".$q."%') OR (t2.username LIKE '%".$q."%') OR (t2.industry LIKE '%".$q."%') OR (t2.slogan LIKE '%".$q."%') OR (t2.location LIKE '%".$q."%')"

sorry for the formatting on the last query, SO messed it up when pasting.

4

2 回答 2

2

您可以尝试使用 UNION,但为每个列名指定一个别名。确保它们的顺序相同。请参见下面的示例:

SELECT first_name col1, last_name col2 etc etc FROM seekers 
    WHERE (first_name LIKE '%".$q."%') 
        OR (last_name LIKE '%".$q."%') 
        OR (username LIKE '%".$q."%') 
        OR (school LIKE '%".$q."%') 
        OR (major LIKE '%".$q."%') 
        OR (employer LIKE '%".$q."%') 
        OR (position LIKE '%".$q."%') 
        OR (background LIKE '%".$q."%') 
        OR (interests LIKE '%".$q."%') 
        OR (skills LIKE '%".$q."%')
    ORDER BY id DESC
UNION
SELECT company_name col1, username col2, etc etc FROM companies 
    WHERE (company_name LIKE '%".$q."%') 
        OR (username LIKE '%".$q."%') 
        OR (industry LIKE '%".$q."%') 
        OR (slogan LIKE '%".$q."%')
        OR (location LIKE '%".$q."%') 
    ORDER BY id

这会将同一结果集中的列匹配在一起。即,first_name 和 company_name 将显示在 col1 中,依此类推。

笔记

  1. 使用 UNION,您需要在两个查询中选择相同数量的列。
  2. 您需要匹配数据类型
  3. 如果您需要伪造列,请使用 null
于 2013-03-01T23:07:32.833 回答
1

当您使用 UNION 时,您需要确保每个 SELECT 中的字段数相等。所以在你上面的例子中你不应该使用'Select *' - 做这样的事情:

选择 t1.field1, t1.field2, t1.field3, t1.field4, t1.field5, t1.field6
From .....
Where .....

联盟

选择 t2.field1, t2.field2, t2.field3, t2.field4, '', ''
From .....
Where .....

如果每个 Select 中的字段数不同,请添加一些空白以使其均匀。

于 2013-03-01T23:04:38.240 回答