Jump to content

max get wrong record


sniperscope

Recommended Posts

Hello

I am crazy and very close to broke my monitor.

I want to get max record from my table by using max() but result give me wrong title, content ..etc

 

Here is my entire table

INSERT INTO `news_event` (`news_id`, `shop_id`, `news_title`, `news_body`, `news_date`, `news_picture`, `news_icon`, `area`) VALUES
(3, 31,  'title for news_id 3', 'contents for news_id 3', 1335498042, 'news_4f9a153aa2ee7.jpg', 'even', 'kansai'),
(5, 32,  'title for news_id 5', 'contents for news_id 5', 1335499227, 'news_4f9a19dbd95f7.jpg', 'even', 'kansai'),
(6, 33,  'title for news_id 6', 'contents for news_id 6', 1335499944, 'news_4f9a1ca8c8870.jpg', 'even', 'kansai'),
(7, 1,   'title for news_id 7',  'contents for news_id 7',  1335515467, 'news_4f9a594b36886.jpg', 'disc', 'kansai'),
(8, 2,   'title for news_id 8',  'contents for news_id 8',  1335517136, 'news_4f9a5fd0aa413.jpg', 'disc', 'kansai'),
(9, 33,  'title for news_id 9', 'contents for news_id 9', 1335524124, 'news_4f9a7b1beca91.jpg', 'even', 'kansai'),
(10, 33, 'title for news_id 10', 'contents for news_id 10', 1335525670, 'news_4f9a8126ca7c0.jpg', 'even', 'kansai'),
(11, 33, 'title for news_id 11', 'contents for news_id 11', 1335578508, 'news_4f9b4f8cb1d2c.jpg', 'even', 'kansai'),
(12, 13, 'title for news_id 12', 'contents for news_id 12', 1335580420, 'news_4f9b57041709a.jpg', 'disc', 'kansai'),
(13, 31, 'title for news_id 13', 'contents for news_id 13', 1335612652, 'news_4f9bd4ec5606c.jpg', 'even', 'kansai'),
(14, 3,  'title for news_id 14',  'contents for news_id 14',  1335849216, 'news_4f9f71005ff65.jpg', 'even', 'kansai'),
(15, 7,  'title for news_id 15',  'contents for news_id 15',  1335866241, 'news_4f9fb3816c2b7.jpg', 'even', 'kansai'),
(16, 20, 'title for news_id 16', 'contents for news_id 16', 1335935205, 'news_4fa0c0e5d4e7f.jpg', 'disc', 'kansai'),
(17, 22, 'title for news_id 17', 'contents for news_id 17', 1335941694, 'news_4fa0c0e624e4f.jpg', 'even', 'kansai'),
(18, 27, 'title for news_id 18', 'contents for news_id 18', 1335942440, 'news_4fa0dd2803ebe.jpg', 'even', 'kansai'),
(19, 22, 'title for news_id 19', 'contents for news_id 19', 1335942585, 'news_4fa0ddb9e1d94.jpg', 'even', 'kansai'),
(43, 15, 'title for news_id 43', 'contents for news_id 43', 1336370423, 'news_4fa764f74e627.jpg', 'news', 'kansai'),
(42, 15, 'title for news_id 42', 'contents for news_id 42', 1336314946, 'news_4fa68c42b9ce7.jpg', 'even', 'kansai'),
(41, 15, 'title for news_id 41', 'contents for news_id 41', 1336307933, 'news_4fa670dd33875.jpg', 'even', 'kansai'),
(40, 15, 'title for news_id 40', 'contents for news_id 40', 1336304857, 'news_4fa664d8f0fb5.jpg', 'even', 'kansai'),
(37, 32, 'title for news_id 37', 'contents for news_id 37', 1336189002, 'news_4fa4a04a5ff6e.jpg', 'even', 'kansai'),
(38, 33, 'title for news_id 38', 'contents for news_id 38', 1336189330, 'news_4fa4a192a58ad.jpg', 'even', 'kansai'),
(39, 15, 'title for news_id 39', 'contents for news_id 39', 1336210819, 'news_4fa4f5839b0be.jpg', 'even', 'kansai'),
(35, 15, 'title for news_id 35', 'contents for news_id 35', 1336138506, 'news_4fa3db0a455a0.jpg', 'news', 'kansai'),
(36, 15, 'title for news_id 36', 'contents for news_id 36', 1336141371, 'news_4fa3e63b2f60f.jpg', 'news', 'kansai'),
(34, 15, 'title for news_id 34', 'contents for news_id 34', 1335976993, 'news_4fa164210812e.jpg', 'even', 'kansai');

 

And this my query:

SELECT `news_id` , `shop_id` , `news_title` , `news_body` , max( `news_date` ) , `news_picture` , `news_icon` , `area`
FROM news_event
WHERE area = 'kansai'
GROUP BY shop_id
ORDER BY `max(``news_date``)` DESC

 

My another query:

SELECT a.*, s.shop_name 
FROM shop_master s 
LEFT JOIN (SELECT news_id, shop_id, news_title, news_body, news_picture, news_icon, MAX(news_date) AS news_date FROM news_event GROUP BY shop_id) a ON s.id = a.shop_id 
WHERE s.shop_area LIKE '%kansai%' 
GROUP BY a.shop_id 
ORDER BY a.news_date DESC

 

I can type 100 different queries but i have no idea why all of them keep getting me wrong order.

I am trying to get correct order about 4 hours.

 

Please help me out.

Link to comment
https://forums.phpfreaks.com/topic/262194-max-get-wrong-record/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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