help w/sql query restructuring

They have: 461 posts

Joined: Jul 2003

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's picture

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?

They have: 461 posts

Joined: Jul 2003

there is no tid in users. that's it's problem.

Suzanne's picture

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.

They have: 461 posts

Joined: Jul 2003

yeah. i don'tknow how to restructure the query so that it will work

Mark Hensler's picture

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.