help w/sql query restructuring
i need help modifying a mysql select statement to get the CORRECT username. the way i have it now it goes by the uid in the forum, i want the uid on the post table.
below is a print out of the cli when running the first construction, finding out which it was using, and then what happened when i tried to fix it.
Quote: mysql> select users.username,threads.locked,threads.fid,threads.tid,fal from posts left join threads using (tid) left join forums using (fid) natural join users where pid=24;
+----------+--------+-----+-----+-----+
| username | locked | fid | tid | fal |
+----------+--------+-----+-----+-----+
| Neo | 1 | 2 | 16 | 0 |
+----------+--------+-----+-----+-----+
1 row in set (0.16 sec)mysql> select uid from threads where tid=16;
+-----+
| uid |
+-----+
| 28 |
+-----+
1 row in set (0.00 sec)mysql> select uid from forums where fid=2;
+-----+
| uid |
+-----+
| 1 |
+-----+
1 row in set (0.00 sec)mysql> select uid from posts where pid=24;
+-----+
| uid |
+-----+
| 28 |
+-----+
1 row in set (0.00 sec)mysql> select users.username,threads.locked,threads.fid,threads.tid,fal from posts natural join users left join threads using (tid) left join forums using (fid) where pid=24;
ERROR 1054: Unknown column 'users.tid' in 'on clause'
mysql> select users.username,threads.locked,threads.fid,threads.tid,fal from posts left join users using (uid) left join threads using (tid) left join forums using (fid) where pid=24;
ERROR 1054: Unknown column 'users.tid' in 'on clause'
POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.
Suzanne posted this at 00:44 — 24th April 2004.
She has: 5,507 posts
Joined: Feb 2000
Sooo, you have a column named "tid" in the users table? Wow is this ever a good example of why using sensible names helps people troubleshoot. fid,pid,tid?!
forum_id == fid
thread_id == tid
post_id == pid
So, does users.thread_id exist? If not, then it can't join on that, right?
m3rajk posted this at 03:06 — 24th April 2004.
They have: 461 posts
Joined: Jul 2003
there is no tid in users. that's it's problem.
Suzanne posted this at 03:31 — 24th April 2004.
She has: 5,507 posts
Joined: Feb 2000
Okay, and you want a query that will work, then, is that it? I'm a little confused about what your question is.
m3rajk posted this at 19:00 — 24th April 2004.
They have: 461 posts
Joined: Jul 2003
yeah. i don'tknow how to restructure the query so that it will work
Mark Hensler posted this at 04:20 — 25th April 2004.
He has: 4,048 posts
Joined: Aug 2000
Try specifiying an ON cluase...
SELECT ... FROM posts LEFT JOIN users ON users.uid=posts.tid ...
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.