Forum Moderators: coopster
My question is both about mySQL and PHP. I have two tables and the first one contains 2 main fields- 'user_id' and 'username'. The second table contains 2 main fields- 'poster_id' and 'message'.
And what I need to do is lookup the user_id from the first table with the username, then pull * from the message_table if the poster_id matches the value pulled from the first table.
Now, I got this working without a problem at all but I had to use 2 queries to do it (1- pull the user_id from the first table by matching it with the username, then 2- using the first id, pull everything from the second table that matches it). Is there an easy way to do those 2 steps in 1 query? I know there must be some way since this seems like a fairly simple and routine problem, but I can't find it online or in any books I have here.
Any help would be appreciated. Thanks again.
Michael Kennedy
$sql =
"SELECT * from message_table
INNER JOIN [mysql.com] user_table ON message_table.poster_id = user_table.user_id
WHERE username = '$username'";
What's wrong with this quesry which should do the same thing?
$query = "SELECT * FROM $message_table WHERE `section` = '$section' AND `poster_id` = (SELECT `user_id` FROM '$user_table' WHERE `username` = '$member' LIMIT 1) SORT BY date DESC";
That seems to follow the syntax correctly, but I can't figure out why it isn't returning any values.
The two queries that I'd like to combine are:
$query = "SELECT * FROM $user_table WHERE `username` = '$member' LIMIT 1";
$query = "SELECT * FROM $message_table WHERE `poster_id` = '$user_data[user_id]' AND `section` = '$section' ORDER BY date DESC";
I hope that's understandable. Any more help is appreciated since I'm wanting to make things as efficient as possible.
-Michael Kennedy
Don't surround your table columns with quotation marks, you'll get a syntax error and your query will not be executed successfully. Assuming your
user_table has unique username entries, you won't use the LIMIT clause:
$sql =
"SELECT * FROM message_table
INNER JOIN user_table ON message_table.poster_id = user_table.user_id
WHERE username = '$member' AND section = '$section' ORDER BY date DESC";
If you only want a couple of fields from one table, but all fields from another:
SELECT table1.field1, table1.field2, table2.* FROM ...
Thanks a bunch. From what I've read, inner joins can be replaced with subqueries and I kinda like the syntax of subqueries better (nevermind that fact that I havent yet gotten one to work- that's just a minor detail). Besides the fact that subqueries are only supported in versions newer than 4.1, is there a reason to not use subqueries? They seem to be a lot more intuitive and simpler (more simple?) to understand.
MK
The main advantages of subqueries are:
- They allow queries which are structured so that it's possible to isolate each part of a statement.
- They provide alternative ways to perform operations which would otherwise require complex joins and unions.
- They are, in many people's opinion, readable. Indeed, it was the innovation of subqueries which gave people the original idea of calling the early
"Structured Query Language".SQL
$query = "SELECT * FROM $message_table WHERE section = '$section' AND poster_id = (SELECT user_id FROM $user_table WHERE username = '$member' LIMIT 1) ORDER BY date DESC";
My problem was stemming from me using "SORT BY" instead of "ORDER BY". I'll just have to look up the difference since "SORT BY" has always worked for me in every case except this one.
Thanks a lot for the help, coopster. I can finally move on to another problem now. :)
MK