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.