Forum Moderators: coopster
From these tables I'd like to query the subjects from subject-table and the date that is the nearest in the future from this day from the date-table. The subjects should be ordered by date (Note: This was posted 19.2.2008):
Subject 1 Date 01.03.2008, Time 12.00. (Nearest to the future)
Subject 3 Date 01.03.2008, Time 13.00. (Next to the future)
Subject 2 Date 01.01.2008, Time 10.00. (Date in the past)
Subject 4 Date NOT SET
This is the MySQL statement I'm currently using (somewhat abstracted)
SELECT DISTINCT s.id, s.type, s.name, s.status
FROM subject s, date d
WHERE s.type='".$sType."' AND s.status=1
ORDER BY d.date, d.time
I also want to print out only the subjects of certain type and only active ones. The problem is that the Subjects can have multiple dates (Subject 2 has dates 01.01.2008, 01.03.2008, 02.03.2008) but the one printed is the first inserted (ie. has the lowest automatic id in date table).
If anyone has any ideas, I'd be grateful.
I managed to fix this and I'm currently using LEFT JOIN on the tables
SELECT DISTINCT s.id, s.type, s.name, s.status
FROM subject s
LEFT JOIN date d
ON s.id = d.subject_id
WHERE s.type='".$sType."' AND s.status=1
ORDER BY d.date, d.time DESC";
The problem I'm having currently is that the subject that does not have a date is printed first and the subject that has date in the past is after this. Last comes the subjects with dates in the future. As such:
Subject 4 Date NOT SET
Subject 2 Date 01.01.2008, Time 10.00. (Date in the past)
Subject 1 Date 01.03.2008, Time 12.00. (Nearest to the future)
Subject 3 Date 01.03.2008, Time 13.00. (Next to the future)
Ideas would be appreciated.
secondly, you are ordering by DATE and TIME therefore the subjects which do not have any date will appear first. you got to mention date for them. If you are using datetime column to order your data then you MUST STORE date and time for each record...
one more thing, you are using DATE as column name in your table, I am confused why doesn't mysql produce an error because I think it is reserved word... or it is reserved in php and not mysql?
The answer to your first observation is similar. The "time" (which I'm quite sure is reserved also) is actually a starting time and there is an ending time. Times and dates in the "date" -table are input by users. Users don't have to input any dates on their subjects. I do use datetimes on timestamps that aren't edited by users.
I was hoping that the subjects could be ordered the way I want with one miraclous MySQL query. Now I'm thinking of using two separate querys, the first one gets all the subjects that have a date in the future and the second one gets all the subjects. After this I'll compare the Id's on the returned selections and first print the subjects with dates (from the first query) and after that the others (minus the ones with dates) in some order.
If someone managed to piece together what I'm doing and has better ideas, I'm all ears.
[edited by: deMorte at 9:22 am (utc) on Feb. 21, 2008]