Hello I have a problem with fetching required rows from MySQL table correctly within date interval.
My table has had such structure originally (yes with unixtimestamps not mysql dates):
id name departures depmin depmax
-------------------------------------------------------------------------
1 a 1327608000 1327608000 1327608000
2 b 1321646400,1322856000, 1321646400 1337976000
1325880000,1327694400,
1329508800,1330718400,
1331928000,1332532800,
1333137600,1333742400,
1334347200,1334952000,
1336161600,1336766400,
1337371200,1337976000
3 c 1315166400,1316894400, 1315166400 1327780800
1317758400,1318968000,
1319918400,1320004800,
1320091200,1320177600,
1321387200,1324152000,
1325448000,1325534400,
1325620800,1327780800
------------------------------------------------------------------------
My task is to get all 3 rows with departure between 1327536001 and 1327881601 but if I query like so:
SELECT * FROM exampletable WHERE depmin >= 1327536001 AND depmax <= 1327881601
I only get the first row (with id 1 and name a). So I’m totally confused how to get all three rows between this example interval?
Please advise how to construct my where query or how to reformat the table.
If I understand your question correctly, you want to get all the
id‘s where any timestamps from columndeparturesfall within specific interval.Unfortunally, there is no simple way to achieve that. You’ll have to either write an extensive stored procedure/function (involving parsing string from
departurescolumn, putting results in array variable or temporary table, comparing them to your borders etc.) or you’ll have to redesign your database heavily.Personally, I’d suggest to move data from
departurescolumn into separate table, one departure time per row, like this:Then you will be able to satisfy your original requirements with query like