Jump to content

Recommended Posts

This is my query:

 

SELECT buddy_id

FROM buddy

LEFT JOIN (SELECT `user_id`,`user_name`,`user_lastvisit`)

FROM users

ON buddy.buddy_id = users.user_id

ORDER BY `order`, `id`

 

This is my error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM users ON buddy.buddy_id = users.user_id ORDER BY' at line 4

 

Help please  ;)

Link to comment
https://forums.phpfreaks.com/topic/90461-join-help/
Share on other sites

Try:

 

SELECT buddy_id
FROM buddy
LEFT JOIN users
ON buddy.buddy_id = users.user_id
ORDER BY `order`, `id`

 

I don't know if that order by will work unless you put those columns in the select list though.

 

Wolphie, the default ordering is ascending.  There's no need to specify.

Link to comment
https://forums.phpfreaks.com/topic/90461-join-help/#findComment-463780
Share on other sites

Try:

 

SELECT buddy_id
FROM buddy
LEFT JOIN users
ON buddy.buddy_id = users.user_id
ORDER BY `order`, `id`

 

I don't know if that order by will work unless you put those columns in the select list though.

 

Wolphie, the default ordering is ascending.  There's no need to specify.

Problem is, I only want `user_id`,`user_name`,`user_lastvisit` from the users field (the whole point of this join is to reduce server strain).

Link to comment
https://forums.phpfreaks.com/topic/90461-join-help/#findComment-463783
Share on other sites

I don't think that will help.  SQL optimizers are very good, and can do that kind of optimization just by examining the select list, and seeing that you don't need all those extra rows.

 

I'll suggest this thread be moved to the mysql forum so the experts can take a look.  I may be wrong.

Link to comment
https://forums.phpfreaks.com/topic/90461-join-help/#findComment-463790
Share on other sites

This is a working script, now I just need help making it so I can select user_name and user_lastvisit from it:

 

SELECT `buddy_id`,`buddy_order`

FROM buddy

JOIN users (this is where I try to put the select statement, but it doesn't work...)

ON buddy.buddy_id = users.user_id

WHERE users.user_id = buddy.buddy_id

GROUP BY `buddy_id`

ORDER BY `buddy_order`, `buddy_id`

 

Link to comment
https://forums.phpfreaks.com/topic/90461-join-help/#findComment-463796
Share on other sites

Got it to work! I didn't know I could SELECT columns from two different tables

 

SELECT `buddy_id`,`buddy_order`, `user_name`, `user_name`, `user_lastvisit`

FROM buddy

JOIN users

ON buddy.buddy_id = users.user_id

WHERE users.user_id = buddy.buddy_id

GROUP BY `buddy_id`

ORDER BY `buddy_order`, `buddy_id`

Link to comment
https://forums.phpfreaks.com/topic/90461-join-help/#findComment-463799
Share on other sites

Is buddy_id and user_id unique?  If so you can drop the group by.  You can also drop the where condition, it doesn't do anything.  So:

 

SELECT `buddy_id`,`buddy_order`, `user_name`, `user_lastvisit`
FROM buddy
JOIN users
ON buddy.buddy_id = users.user_id
ORDER BY `buddy_order`, `buddy_id`

 

If you need to specify which table to select a column from, you can do it like this:

 

SELECT `buddy`.`buddy_id` ... 

Link to comment
https://forums.phpfreaks.com/topic/90461-join-help/#findComment-463811
Share on other sites

For a little more clarity on your SQL statements it's worth using aliases on your tables (just in case someone else picks up your SQL statement and doesn't know which tables provide which columns). Thus I give you the following:

SELECT b.`buddy_id` as 'id'
      ,b.`buddy_order` as 'order'
      , u.`user_name` as 'name'
      , u.`user_lastvisit` as 'lastvisit'
FROM buddy b
JOIN users u ON b.buddy_id = u.user_id
ORDER BY b.`buddy_order`, b.`buddy_id`

 

This will also help to identify any ambiguous column names (i.e. ones that exist in both tables).

Link to comment
https://forums.phpfreaks.com/topic/90461-join-help/#findComment-463857
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.