dongyange1101 2014-01-08 22:22
浏览 33

以日期分隔的Mysql查询元素组

Im currenty crwaling a website and saving to a DB all the images from a carousel. each of this images have a position, link, image url, etc.

findle with all the info: sqlfiddle.com/#!2/b3920b/2/0 (thanks to Ollie Jones)

So de DB looks kinda like this:

╔═══╦════════════╦═════════════╦═════════════╦═════════════╦═════════════╦═════════════╗
 ║id ║  element   ║  position   ║   title     ║  url_link   ║   url_jpg   ║     date    ║
 ╠═══╬════════════╬═════════════╬═════════════╬═════════════╬═════════════╬═════════════╣
 ║ 1 ║ foo        ║      1      ║    title1   ║     url1    ║    jpg1     ║  20-12-2013 ║
 ║ 2 ║ foo        ║      2      ║    title2   ║     url2    ║    jpg2     ║  20-12-2013 ║
 ║ 3 ║ foo        ║      3      ║    title3   ║     url3    ║    jpg3     ║  20-12-2013 ║
 ║ 4 ║ foo        ║      4      ║    title4   ║     url4    ║    jpg4     ║  20-12-2013 ║
 ║ 5 ║ foo        ║      5      ║    title5   ║     url5    ║    jpg5     ║  20-12-2013 ║
 ║ 6 ║ foo        ║      6      ║    title6   ║     url6    ║    jpg6     ║  20-12-2013 ║
 ║ 7 ║ foo        ║      1      ║    title1   ║     url1    ║    jpg1     ║  21-12-2013 ║
 ║ 8 ║ foo        ║      2      ║    title2   ║     url2    ║    jpg2     ║  21-12-2013 ║
 ║ 9 ║ foo        ║      3      ║    title3   ║     url3    ║    jpg3     ║  21-12-2013 ║
 ║10 ║ foo        ║      4      ║    title4   ║     url4    ║    jpg4     ║  21-12-2013 ║
 ║11 ║ foo        ║      5      ║    title5   ║     url5    ║    jpg5     ║  21-12-2013 ║
 ║12 ║ foo        ║      6      ║    title6   ║     url6    ║    jpg6     ║  21-12-2013 ║
 ║13 ║ foo        ║      1      ║    title1   ║     url1    ║    jpg1     ║  22-12-2013 ║
 ║14 ║ foo        ║      2      ║    title2   ║     url2    ║    jpg2     ║  22-12-2013 ║
 ║15 ║ foo        ║      3      ║    title3   ║     url3    ║    jpg3     ║  22-12-2013 ║
 ║16 ║ foo        ║      4      ║    title4   ║     url4    ║    jpg4     ║  22-12-2013 ║
 ║17 ║ foo        ║      5      ║    title5   ║     url5    ║    jpg5     ║  22-12-2013 ║
 ║18 ║ foo        ║      6      ║    title6   ║     url6    ║    jpg6     ║  22-12-2013 ║
 ║.. ║    ...     ║     ...     ║     ...     ║     ...     ║     ...     ║      ...    ║
 ╚═══╩════════════╩═════════════╩═════════════╩═════════════╩═════════════╩═════════════╝

And then in my reports, I query the database to show me what element were present in the carousel with start date and end date, if there is a change in position, title, url of the link of jpg then it doest have to show me any range... for instance, the query of the DB before should look like:

 ╔═════════╦═══════════╦═════════════╦═════════════╦═══════════╦══════════════════════════╗
 ║element  ║  position ║   title     ║  url_link   ║   url_jpg ║           date           ║
 ║═════════╬═══════════╬═════════════╬═════════════╬═══════════╬══════════════════════════╣
 ║ foo     ║      1    ║    title1   ║     url1    ║    jpg1   ║ 20-12-2013 to 22-12-2013 ║
 ║ foo     ║      2    ║    title2   ║     url2    ║    jpg2   ║ 20-12-2013 to 22-12-2013 ║
 ║ foo     ║      3    ║    title3   ║     url3    ║    jpg3   ║ 20-12-2013 to 22-12-2013 ║
 ║ foo     ║      4    ║    title4   ║     url4    ║    jpg4   ║ 20-12-2013 to 22-12-2013 ║
 ║ foo     ║      5    ║    title5   ║     url5    ║    jpg5   ║ 20-12-2013 to 22-12-2013 ║
 ║ foo     ║      6    ║    title6   ║     url6    ║    jpg6   ║ 20-12-2013 to 22-12-2013 ║
 ╚═════════╩═══════════╩═════════════╩═════════════╩═══════════╩══════════════════════════╝

The above uses this query:

mysql_query("
 SELECT id, element, position, title, url_link, url_jpg, 
        (case when min(date) <> max(date) 
         then concat(min(date), ' to ', max(date)) 
         else min(date) end) as date
    FROM table 
   GROUP BY element, posistion, title, url_link, url_jpg 
   ORDER BY element, date ASC, position ASC
")

Now the problem is that when all elements change BUT one, like this:

new DB:

 ╔═══╦════════════╦═════════════╦═════════════╦═════════════╦═════════════╦═════════════╗
 ║id ║  element   ║  position   ║   title     ║  url_link   ║   url_jpg   ║     date    ║
 ╠═══╬════════════╬═════════════╬═════════════╬═════════════╬═════════════╬═════════════╣
 ║ 1 ║ foo        ║      1      ║    title1   ║     url1    ║    jpg1     ║  20-12-2013 ║
 ║ 2 ║ foo        ║      2      ║    title2   ║     url2    ║    jpg2     ║  20-12-2013 ║
 ║ 3 ║ foo        ║      3      ║    title3   ║     url3    ║    jpg3     ║  20-12-2013 ║
 ║ 4 ║ foo        ║      4      ║    title4   ║     url4    ║    jpg4     ║  20-12-2013 ║
 ║ 5 ║ foo        ║      5      ║    title5   ║     url5    ║    jpg5     ║  20-12-2013 ║
 ║ 6 ║ foo        ║      6      ║    title6   ║     url6    ║    jpg6     ║  20-12-2013 ║
 ║ 7 ║ foo        ║      1      ║    title1   ║     url1    ║    jpg1     ║  21-12-2013 ║
 ║ 8 ║ foo        ║      2      ║  newtitle2  ║     url2    ║    jpg2     ║  21-12-2013 ║
 ║ 9 ║ foo        ║      3      ║  newtitle3  ║     url3    ║    jpg3     ║  21-12-2013 ║
 ║10 ║ foo        ║      4      ║  newtitle4  ║     url4    ║    jpg4     ║  21-12-2013 ║
 ║11 ║ foo        ║      5      ║  newtitle5  ║     url5    ║    jpg5     ║  21-12-2013 ║
 ║12 ║ foo        ║      6      ║  newtitle6  ║     url6    ║    jpg6     ║  21-12-2013 ║
 ║13 ║ foo        ║      1      ║     title1  ║     url1    ║    jpg1     ║  22-12-2013 ║
 ║14 ║ foo        ║      2      ║  newtitle2  ║     url2    ║    jpg2     ║  22-12-2013 ║
 ║15 ║ foo        ║      3      ║  newtitle3  ║     url3    ║    jpg3     ║  22-12-2013 ║
 ║16 ║ foo        ║      4      ║  newtitle4  ║     url4    ║    jpg4     ║  22-12-2013 ║
 ║17 ║ foo        ║      5      ║  newtitle5  ║     url5    ║    jpg5     ║  22-12-2013 ║
 ║18 ║ foo        ║      6      ║  newtitle6  ║     url6    ║    jpg6     ║  22-12-2013 ║
 ║.. ║    ...     ║     ...     ║     ...     ║     ...     ║     ...     ║      ...    ║
 ╚═══╩════════════╩═════════════╩═════════════╩═════════════╩═════════════╩═════════════╝

The same query will show me:

 ╔═════════╦═══════════╦═════════════╦═════════════╦═══════════╦═════════════╗
 ║element  ║  position ║   title     ║  url_link   ║   url_jpg ║   date      ║
 ║═════════╬═══════════╬═════════════╬═════════════╬═══════════╬═════════════╣
 ║ foo     ║      2    ║    title2   ║     url2    ║    jpg2   ║ 20-12-2013  ║
 ║ foo     ║      3    ║    title3   ║     url3    ║    jpg3   ║ 20-12-2013  ║
 ║ foo     ║      4    ║    title4   ║     url4    ║    jpg4   ║ 20-12-2013  ║
 ║ foo     ║      5    ║    title5   ║     url5    ║    jpg5   ║ 20-12-2013  ║
 ║ foo     ║      6    ║    title6   ║     url6    ║    jpg6   ║ 20-12-2013  ║
 ╚═════════╩═══════════╩═════════════╩═════════════╩═══════════╩═════════════╝
 ╔═════════╦═══════════╦═════════════╦═════════════╦═══════════╦══════════════════════════╗
 ║element  ║  position ║   title     ║  url_link   ║   url_jpg ║           date           ║
 ║═════════╬═══════════╬═════════════╬═════════════╬═══════════╬══════════════════════════╣
 ║ foo     ║      1    ║    title1   ║     url2    ║    jpg2   ║ 20-12-2013 to 22-12-2013 ║
 ╚═════════╩═══════════╩═════════════╩═════════════╩═══════════╩══════════════════════════╝
 ╔═════════╦═══════════╦═════════════╦═════════════╦═══════════╦══════════════════════════╗
 ║element  ║  position ║   title     ║  url_link   ║   url_jpg ║           date           ║
 ║═════════╬═══════════╬═════════════╬═════════════╬═══════════╬══════════════════════════╣
 ║ foo     ║      2    ║    title2   ║     url2    ║    jpg2   ║ 21-12-2013 to 22-12-2013 ║
 ║ foo     ║      3    ║    title3   ║     url3    ║    jpg3   ║ 21-12-2013 to 22-12-2013 ║
 ║ foo     ║      4    ║    title4   ║     url4    ║    jpg4   ║ 21-12-2013 to 22-12-2013 ║
 ║ foo     ║      5    ║    title5   ║     url5    ║    jpg5   ║ 21-12-2013 to 22-12-2013 ║
 ║ foo     ║      6    ║    title6   ║     url6    ║    jpg6   ║ 21-12-2013 to 22-12-2013 ║
 ╚═════════╩═══════════╩═════════════╩═════════════╩═══════════╩══════════════════════════╝

And I want it to show me this:

 ╔═════════╦═══════════╦═════════════╦═════════════╦═══════════╦════════════╗
 ║element  ║  position ║   title     ║  url_link   ║   url_jpg ║  date      ║
 ║═════════╬═══════════╬═════════════╬═════════════╬═══════════╬════════════╣
 ║ foo     ║      1    ║    title1   ║     url1    ║    jpg1   ║ 20-12-2013 ║
 ║ foo     ║      2    ║    title2   ║     url2    ║    jpg2   ║ 20-12-2013 ║
 ║ foo     ║      3    ║    title3   ║     url3    ║    jpg3   ║ 20-12-2013 ║
 ║ foo     ║      4    ║    title4   ║     url4    ║    jpg4   ║ 20-12-2013 ║
 ║ foo     ║      5    ║    title5   ║     url5    ║    jpg5   ║ 20-12-2013 ║
 ║ foo     ║      6    ║    title6   ║     url6    ║    jpg6   ║ 20-12-2013 ║
 ╚═════════╩═══════════╩═════════════╩═════════════╩═══════════╩════════════╝

 ╔═════════╦═══════════╦═════════════╦═════════════╦═══════════╦══════════════════════════╗
 ║element  ║  position ║   title     ║  url_link   ║   url_jpg ║           date           ║
 ║═════════╬═══════════╬═════════════╬═════════════╬═══════════╬══════════════════════════╣
 ║ foo     ║      1    ║    title1   ║     url1    ║    jpg1   ║ 21-12-2013 to 22-12-2013 ║
 ║ foo     ║      2    ║ newtitle2   ║     url2    ║    jpg2   ║ 21-12-2013 to 22-12-2013 ║
 ║ foo     ║      3    ║ newtitle3   ║     url3    ║    jpg3   ║ 21-12-2013 to 22-12-2013 ║
 ║ foo     ║      4    ║ newtitle4   ║     url4    ║    jpg4   ║ 21-12-2013 to 22-12-2013 ║
 ║ foo     ║      5    ║ newtitle5   ║     url5    ║    jpg5   ║ 21-12-2013 to 22-12-2013 ║
 ║ foo     ║      6    ║ newtitle6   ║     url6    ║    jpg6   ║ 21-12-2013 to 22-12-2013 ║
 ╚═════════╩═══════════╩═════════════╩═════════════╩═══════════╩══════════════════════════╝

(To separate them into tables in the php file I make a new table when date changes)

So I have no idea how could I change the query so the element with title1 separates into the two tables since there was a change in the GROUP date. :(

Hope is well explain, this is really giving me a headache. thank you in advance.

  • 写回答

1条回答 默认 最新

  • dtio35880438 2014-01-09 00:03
    关注

    I believe the problem you are seeing is due to ORDERING problems with your resultset's date column. My Fiddle (here) uses actual DATE values, not dd-mm-yyyy values, because we want them to sort correctly. http://sqlfiddle.com/#!2/b3920b/4/0 Also, I'm using this

        ORDER BY element, MIN(date), MAX(date), position ASC
    

    Notice that rows 1, 7, 13 of your second dataset will GROUP together into a single result row with a date range of 2013-12-20 to 2013-12-22.

    There's a minor mistake in your SQL.. you don't want to

         SELECT id, ....
    

    because it's not mentioned in your GROUP BY. See here: http://sqlfiddle.com/#!2/b3920b/3/0

    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装