CanMan2004 Posted December 14, 2006 Share Posted December 14, 2006 Hi allI 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 advanceDave Quote Link to comment Share on other sites More sharing options...
craygo Posted December 14, 2006 Share Posted December 14, 2006 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 Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 14, 2006 Author Share Posted December 14, 2006 there not linked, but both contain a username and password that I want to show on screen, so by doing 2 tables in one query, it should return all the users that I hold Quote Link to comment Share on other sites More sharing options...
swatisonee Posted December 14, 2006 Share Posted December 14, 2006 Umm this probaby wont work. just saw your 2nd post.Try using UNION if theres a common field. I think this post is what you are lookgin for ?http://www.phpfreaks.com/forums/index.php/topic,110449.0.html Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 14, 2006 Author Share Posted December 14, 2006 The tables both already have data, so I cant combine them, the structure looks likeusers tableID NAME USERNAME PASSWORD DEPARTMENTandadmin tableID NAME USERNAME PASSWORD ACCESS CODEand I want to query both Quote Link to comment Share on other sites More sharing options...
craygo Posted December 14, 2006 Share Posted December 14, 2006 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 Quote Link to comment Share on other sites More sharing options...
craygo Posted December 14, 2006 Share Posted December 14, 2006 if the names are the same you can always add a field to the admin table called usr_id and then pull the user id from the user table and populate the admin table.Ray Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 14, 2006 Author Share Posted December 14, 2006 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 1The 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? Quote Link to comment Share on other sites More sharing options...
swatisonee Posted December 14, 2006 Share Posted December 14, 2006 But how come the two myid values are different in the 2nd code? Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 14, 2006 Author Share Posted December 14, 2006 Sorry that was a mistake, the ones I want to query all have the same code f65hb Quote Link to comment Share on other sites More sharing options...
craygo Posted December 14, 2006 Share Posted December 14, 2006 For future reference, if you want to get multiple values from a table use [code]SELECT * FROM admin, users WHERE admin.myid IN ('f65hb', '9ijh55')";[/code]Ray Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 14, 2006 Author Share Posted December 14, 2006 Thanks Ray, i'll store that.Do you have any ideas on my problem? Quote Link to comment Share on other sites More sharing options...
swatisonee Posted December 15, 2006 Share Posted December 15, 2006 i assume MYID exists in both tables with correct case and defined as int right ? Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 17, 2006 Author Share Posted December 17, 2006 Hi allYes, MYID exists in both tables.Can anyone help me with this as im totally stuck. Quote Link to comment Share on other sites More sharing options...
swatisonee Posted December 19, 2006 Share Posted December 19, 2006 Try [code]$sql = " SELECT * FROM admin, users WHERE (admin.myid = 'f65hb' AND admin.myid = '9ijh55') ";echo $sql;$result = mysql_query($sql) or die (mysql_error());[/code]and post the result Quote Link to comment Share on other sites More sharing options...
corbin Posted December 19, 2006 Share Posted December 19, 2006 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'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.