开发者

How to do this simple mysql select statement

开发者 https://www.devze.com 2023-03-31 07:01 出处:网络
I\'m learning more about SQL and have found it helpful already in solving a problem I was going to have to post-fix in PHP, now I\'ve found mysql can do it on retrival.

I'm learning more about SQL and have found it helpful already in solving a problem I was going to have to post-fix in PHP, now I've found mysql can do it on retrival.

The query I have is this.

    SELECT *
                            FROM  `messages` 
                            LEFT JOIN  `users` ON messages.message_by = users.id
                            WHERE job_id =  '135'
                            ORDER BY message_date DESC

But I want to do this now,开发者_高级运维

    SELECT DATE_ADD(`message_date`, INTERVAL 6 HOUR) AS `message_date`, *
                            FROM  `messages` 
                            LEFT JOIN  `users` ON messages.message_by = users.id
                            WHERE job_id =  '135'
                            ORDER BY message_date DESC

Where the date_add part fixes a time zone difference problem, but as I've put that in the select part of the statement I now don't know how to use the , * (and then everything else) it's not working. I don't want to have to list every field I'm after retrieving just because I've added this date fix...

What is a way around it? Thanks.


Just prepend the name of the table and it should work

SELECT DATE_ADD(`message_date`, INTERVAL 6 HOUR) AS `message_date`, messages.*
                            FROM  `messages` 
                            LEFT JOIN  `users` ON messages.message_by = users.id
                            WHERE job_id =  '135'
                            ORDER BY message_date DESC

Also, are you SURE you need to select all of the fields? You can improve speed by only selecting the columns you need.

0

精彩评论

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