Uzoezi Uzoezi - 3 years ago 117
MySQL Question

How to create a query that searches different tables on the same database to log a user into a website?

I'm building a simple login/registration feature, and I'm having a little trouble.

The issue is this, the user system I'm designing is supposed to accommodate different types of users, like (Blue users, Red users, Black users etc). So I was considering porting their different user data to separate database tables, and even giving them separate registration pages, because the content they would view on the site would vary depending on their color.

For example:

Blue Users:

INSERT 'username' INTO `blue` where...


Red Users:

INSERT 'username' INTO `red` where...


But I want to know if there's a way to log them into the site from the same login page without resorting to sending them to different pages when they want to login. I tried the following:

"SELECT `id` FROM `blue`,`red` WHERE `username`...


but it did not work, so I'm asking if there's a way to register the different user types on the same page and log them in on the same page while still giving them their different content.

Answer Source

One problem is probably that the "id" value is an auto-incremented primary key, right? Therefore it would be unique to only the table that they are in, but not over all tables (blue, red, black)

You would then always have to make sure to have unique usernames and ids over all groups or let them select what group they are when they log in and then only load data from that table.

If you have already solved the uniqueness problem, you should be able to simply use the union querys already suggested by other users. You can't use blue, red, because that is short for blue JOIN red, which of course will only result in data where columns with the same name are equal across blue and red.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download