开发者

MySQL Query: right syntax to use for `SELECT users.(foo, bar)`

开发者 https://www.devze.com 2023-01-07 00:00 出处:网络
Here is the MySQL code. SELECT 开发者_运维百科users.(user_id, pic, first_name, last_name, username),

Here is the MySQL code.

SELECT 开发者_运维百科users.(user_id, pic, first_name, last_name, username), 
           comments.(id, user_id, date_created)
FROM users
INNER JOIN comments ON users.user_id = comments.user_id 
WHERE comments.user_id = '$user_id'
GROUP BY comments.date_created

I get the following 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 '(user_id, pic, first_name, last_name, username), comments.(id, user_id, date_created)' at line 1


Try:

SELECT u.user_id, u.pic, u.first_name, u.last_name, u.username, c.id, c.user_id AS comment_user_id, c.date_created
    FROM users u
    INNER JOIN comments c ON u.user_id = c.user_id 
    WHERE c.user_id = '$user_id'
    GROUP BY c.date_created


users.(user_id, pic, first_name, last_name, username)

should be

users.user_id,users.pic, users.first_name, users.last_name, users.username


Your query should be as already posted in two answers, but if you will have problems with JOIN you can simply do it so.

SELECT u.user_id, u.pic, u.first_name, u.last_name, u.username,
       c.id, c.user_id, c.date_created
FROM users u, comments c
WHERE u.user_id = c.user_id AND c.user_id = '$user_id'
GROUP BY c.date_created;

That should clear things up. Sorry for repeating most of the query, but I just hate JOINs ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号