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.