Jump to content

Query 2 tables


CanMan2004

Recommended Posts

Hi all

I have a database called "users" and another called "admins".

At the moment, I have a query to look in the table "users", the query I use is

[code]SELECT * FROM users WHERE area =1[/code]

What I want to do is to look in both the "users" table and also look in "admins" but within the same query. Is this possible or should I just do 2 queries?

Thanks in advance

Dave
Link to comment
https://forums.phpfreaks.com/topic/30654-query-2-tables/
Share on other sites

Are the 2 tables related in any way??

If they are not relative just use 2 queries because there is nothing to relate the 2. Why don't you just flag a user as an admin in the same table. Make a field called group and have it say user, power, admin, ect...Much easier.

Ray
Link to comment
https://forums.phpfreaks.com/topic/30654-query-2-tables/#findComment-141234
Share on other sites

I guess it should. I never really tried it. LOL just make sure you differentiate the 2 if the field names are the same

[code]$sql = "SELECT user.name as uname, admin.name as aname FROM user, admin WHERE user.area = 1 and admin.area = 1";[/code]

Can try that. Never really tried it though. Seems a little redundant to me.


Ray
Link to comment
https://forums.phpfreaks.com/topic/30654-query-2-tables/#findComment-141241
Share on other sites

Thanks for the help, ive used

[code]SELECT * FROM admin, users WHERE admin.id = '1' AND admin.id = '1'[/code]

Which returns 2 rows with the ID 1

The problem I have is im trying to do the where statement for a code in a field called MYID, so I want to use

[code]SELECT * FROM admin, users WHERE admin.myid = 'f65hb' AND admin.myid = '9ijh55'[/code]

But it wont return anything using that? Is there a reason it returns rows when using the ID number field, but with the MYID field, it wont return anything.

Any ideas?
Link to comment
https://forums.phpfreaks.com/topic/30654-query-2-tables/#findComment-141251
Share on other sites

SELECT * FROM admin,users WHERE users.username = 'corbin' and admin.id = users.id;

This is basically the same as
$q = mysql_query("SELECT * FROM users WHERE username = 'corbin');
$r = mysql_fetch_assoc($q);
$id = $r['ID'];
$q2 = mysql_query("SELECT * FROM admin WHERE ID = '{$id}'");

Only problem is, this assumed that the ids match in both tables... You could always pull it from both tables based on USERNAME though such as...

SELECT * FROM admin,users WHERE users.username = 'corbin' and admin.username = 'corbin';
Link to comment
https://forums.phpfreaks.com/topic/30654-query-2-tables/#findComment-144155
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.