Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Mysql union search or select from 4 tables

Featured Replies

Hey, I'm currently developing a website where it's a job application website.

 

I am currently trying to retrieve messages in a thread system for the messaging system of the website. The problem I'm having is I have jobseekers and employers (different users) stored in two tables, and also a generic users table that link them both.

 

The current query i'm performing is:

 

SELECT userid, userEmail, userAvatar, messageid, senderid, dateSent, timeSent, messageContent, favourite, empName, empDepartment, empIndustry, jsForename, jsSurname, jsEmpStatus
		FROM users
		JOIN messages ON (users.userid = messages.senderid)
		JOIN employers ON (users.userid = employers.empid)
                       JOIN jobseekers ON (jobseekers.jsid = users.userid)
		WHERE recipientid = '2'

 

recipientid is gathered from a session for the user currently logged in, i.e. all the messages sent to that user.

The query works perfectly fine like this:

 

SELECT userid, userEmail, userAvatar, messageid, senderid, dateSent, timeSent, messageContent, favourite, jsForename, jsSurname, jsEmpStatus
		FROM users
		JOIN messages ON (users.userid = messages.senderid)
                       JOIN jobseekers ON (jobseekers.jsid = users.userid)
		WHERE recipientid = '2'

And visa versa, however, if I add the fourth table it returns no results.

There are results in the database as I've manually added them.

 

The other option I have tried is by using a union to perform the search:

 

SELECT userid, userEmail, userAvatar, messageid, senderid, dateSent, timeSent, messageContent, favourite, empName, empDepartment, empIndustry, NULL , NULL , NULL
FROM users
JOIN messages ON ( users.userid = messages.senderid ) 
JOIN employers ON ( users.userid = employers.empid ) 
WHERE recipientid =  '2'
UNION 
SELECT userid, userEmail, userAvatar, messageid, senderid, dateSent, timeSent, messageContent, favourite, NULL , NULL , NULL , jsForename, jsSurname, jsEmpStatus
FROM users
JOIN messages ON ( users.userid = messages.senderid ) 
JOIN jobseekers ON ( jobseekers.jsid = users.userid ) 
WHERE recipientid =  '2'

However, the column names jsForename, jsSurname and jsEmpStatus are returned as null? Therefore I can't target them when filtering results in while statement.

  • Author

You need to learn about joins, below is a pretty comprehensive tutorial.

 

 

http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php

 

Ohh great and how much help was that, if you had of read the question you may have understood my problem? I'm well aware how to join a table(s).

Ohh great and how much help was that, if you had of read the question you may have understood my problem? I'm well aware how to join a table(s).

 

 

I posted that link as it contains information on doing complex joins and performing joins on multiple tables.

 

I'm sorry I didn't figure out the solution for you, I normally do but didn't have time but wanted to help in some way. I don't think there is any need for you to be narky about it.

 

Anyway maybe if you post up your database tables I'll give it a crack.

Edited by FizixRichard

  • Author

Apologies for earlier, stressful times put in in an agitated mood. Been trying to solve this for a while now but I can't figure it out.

 

I've got two sets of users, jobseekers and employers (they both have profiles and messaging functionality) however, I need to pull from the jobseekers or employers table depending on what matches in the where clause.

freshjobs.txt

Right, here goes.

 

The reason I brought up learning about Joins was because different types of join achieve different things; for example left and right joins.

 

Let's say you are trying to retrieve a forum thread:

 

thread table, post table, user table.

 

If you want to return every post for a given threadid, regardless of whether a user record exists, go left join. If you want to ignore any posts that don't have a valid user record (i.e. the user has been deleted) use right join (which forces that join to return a result to be valid).

 

Well thats not entirely true, but it's effectively what you get.

 

A better explanation of inner and outer / left and right joins, is that it defines the direction you are searching the DB in.

 

As in, a LEFT JOIN will show all records from the left table regardless of whether there are matching tables records in the right table.

 

RIGHT JOIN does the same, but in the opposite direction.

 

Anyway, in your context I'd simply use a sequence of left joins. This may not be the BEST method as I've kept it simple, but its a working method that is easy to read:

 

 

The query would look something like this:

 

SELECT messages.*, users.*, employers.*, jobseekers.* FROM messages LEFT JOIN users ON users.userid = messages.senderid LEFT JOIN employers ON employers.empid = users.userid LEFT JOIN jobseekers ON jobseekers.jsid = users.userid WHERE messages.recipientid='2'

 

 

First, I've selected all 4 tables, all fields; which means you could get "field conflicts" so its best to explicitly state the fields you need.

 

Also, you should always state the table before the field when using joins, to prevent confusion. Like users.userid and jobseekers.jsid as opposed to userid and jsid.

 

 

We select from messages where messages.recipientid='whatever_the_id_is'

 

In the middle we do 3 left joins.

 

LEFT JOIN 1 selects any records from users where users.userid = messages.senderid (regardless of whether a user record exists for that senderid, changing this to a right join will fail the query if the user doesn't exist)

 

LEFT JOIN 2 select any records from employers where employers.empid = users.userid (this will have to be a left join as a user could be an employer or jobseeker)

 

LEFT JOIN 3 selects any records from jobseekers where jobseekers.jsid = users.userid (this will have to be a left join as a user could be an employer or jobseeker)

 

 

Then on your display code (where you display the message), you would use logic there to parse the data and decide how and what to display.

Edited by FizixRichard

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.