How to write MySql query with multiple data orders? - php

I am working on an application for an organization.
Here i have some events in database table. Now client requirements for their view are as follow.
First event should be today's event (If there is any)
Then upcoming events should be from todays date. Like if today
01-11-2011 then next event should be of 02-11-2011(if there is any)
and next one should be of 03-11-2011 and so on....
Once all upcoming events have been listed, you can display old
events.
I am just wondering if i can accomplish this task in just one Query. Can you suggest me some query?
Here is table structure.
Currently i have this query.
SELECT * FROM tbl_event ORDER BY event_date DESC;
This application is in Codeignitor framework and MySql is database type.

SELECT ...
...
ORDER BY (event_date = curdate()) DESC, // today's events
(event_date > curdate()) DESC, // future events
event_date DESC; // past events

Something like this maybe?
SELECT * FROM tbl_event ORDER BY event_date DESC WHERE DATE(event_date) = DATE(NOW()) UNION SELECT * FROM tbl_event ORDER BY event_date DESC WHERE event_date >= NOW() UNION SELECT * FROM tlb_event ORDER_BY event_date DESC WHERE event_date < NOW()

Related

Pulling Last Two Dates PHP/MySQL

I have a table in my database which is updated randomly. I'm trying to pull entries by the latest date. This part is simply and I can do it with ease. However, I want to pull the two latest dates.
Example; If my last update was 2015-06-22 and the one before than was 2015-06-12 and the one before then was 2015-06-02. I would want to pull 2015-06-22 and 2015-06-15.
I would use a LIMIT 2, however, there are an unknown amount of items that may have the same date attached.
I haven't tried anything other than the LIMIT 2. After some research, I wasn't able to find anything to reference.
Update
I used SELECT DISTINCT to get the desired results.
SELECT DISTINCT dates FROM table ORDER BY dates DESC LIMIT 2
Will give you the latest 2 dates in the table.
I would have a column set to id, that is auto incremented, and do my query like this:
SELECT * FROM tbl_name ORDER BY `id` DESC LIMIT 2
Crap McAdam you beat me to it!
You can get the latest two dates using LIMIT, like you mentioned:
SELECT latestDates
FROM myTable
ORDER BY dateColumn DESC
LIMIT 2;
And you can join that to your original table to only select rows that occur on those two dates:
SELECT m.*
FROM myTable m
JOIN(
SELECT latestDates
FROM myTable
ORDER BY dateColumn DESC
LIMIT 2) tmp ON tmp.latestDates = m.dateColumn;

Order by date and group by id

I have a list of events in events_table, i store date of event in yyyy-mm-dd format.
I need to sort events by date:
(SELECT * from events_table ORDER BY date DESC)
But if I have more then 1 events on similar date, sorting works wrong.
I want to sort by date and by id, to view correct order(new events first).
I know that good practice is to use a timestamp, but since users can introduce information about event that was a day or two ago and this method not working in my case.
If you want first descending order by date and then ascending order by id then use below-
SELECT * from events_table ORDER BY date DESC, id ASC;
If you want first descending order by date and then descending order by id then use below-
SELECT * from events_table ORDER BY date DESC, id Desc;
If if id is primary key and auto_increment then there is no need to use ordering on date as ordering on id will be enough and will be more optimized.
SELECT * from events_table ORDER BY id Desc;
You can ORDER BY multiple fields.
SELECT * from events_table ORDER BY date DESC, id;

Use multiple tables to find a record MySQL

I'm sorry if the title is missleading, but I didn't know how to sum it up, sorry :(
Basically, I've made a event system that shows events that you can attend.
I got two tables
"events" and "registrations"
The events table contains all information about a single event, datetime of the event, price,, etc...
Everytime a user clicks register on an event, a record is added to the registration table.
A record looks like this: Registrationid, eventid, date, status and userid
Now I use this query to get all passed events:
SELECT * FROM events WHERE Date < CURRENT_DATE() ORDER BY Date ASC
I only want it to show previous events on the user page, if the user has actually attended the event. Is there a way I can check the registration table, and if a record exists for that event; show the event? I can do this is php, but I figured it would be possible via MySQL, though I'm not completely sure.
Is there a query that can handle the job? :)
Sure, that's a pretty simple join operation. Here's how I would do this:
select e.*
from event e join registration r
on (r.event_id=e.event_id)
where r.user_id=123 and e.date < current_date()
order by e.date asc
A couple of other tips:
Avoid using reserved words and data types as column names (e.g. "date")
The default storage engine in MySQL does not support referential integrity. If you need referential integrity, take a peek at InnoDB.
For more on joins in MySQL, check this out: https://dev.mysql.com/doc/refman/5.0/en/join.html
SELECT * FROM events e , registrations r where e.eventid = r.eventid AND e.Date < CURRENT_DATE()
SELECT e.*
FROM events e
JOIN registrations r
ON e.eventid = r.eventid
WHERE Date < CURRENT_DATE()
AND r.userid = <insert id here>
ORDER BY Date ASC
This query would give you all the fields from the events table, where a record in the registrations table has a specific user id.
Example Select with a join with your information:
SELECT * FROM events AS e WHERE e.Date < CURRENT_DATE() JOIN registration AS r ON e.id = r.eventid ORDER BY e.Date ASC
Despite other commenters' suggestion to use a JOIN (which is a decent suggestion), I think it's actually better in this case to use IN plus a subquery:
SELECT *
FROM events
WHERE eventid IN
( SELECT r.eventid
FROM registrations r
WHERE r.userid = ...
)
AND date < CURRENT_DATE()
ORDER
BY date ASC
(replacing the ... with the appropriate user-ID).
Using a JOIN, you can select the registrations rows for the userid, and then get the events data
SELECT
r.*, // Instead of *, you could specify the columns - r.userid, r.status, etc
e.* // Instead of *, you could specify the columns - e.event_name, e.Date, etc
FROM
registrations r
JOIN
events e
ON
r.eventid = e.id
WHERE
r.userid = YOUR_USERID
AND
r.status = ATTENDED
AND
e.Date < CURRENT_DATE()
ORDER BY
e.Date ASC

Show mysql random result

I have a mysql table named events. Some events are featured. I want to randomly display one of the two latest featured events. The field 'timestamp' holds the UNIX timestamp of the event's creation time.
The query looks like this now:
$query = "SELECT * FROM events WHERE featured = 1 ORDER BY timestamp DESC LIMIT 2;";
Is there a way to syntax the query to return just one of those two events and display it right away, or should I go around it with php?
What is recomended here?
Use a ORDER BY RAND() LIMIT 1;, as per MySQL documentation for RAND() (near the bottom of the explanation). I'm not sure if you can do it without the nesting, but it shouldn't be all that expensive given that your nested table only has 2 rows.
SELECT * FROM
(SELECT * FROM events WHERE featured = 1 ORDER BY timestamp DESC LIMIT 2)
ORDER BY RAND() LIMIT 1;
Try:
SELECT * FROM
(SELECT * FROM EVENTS WHERE featured = 1 ORDER BY `timestamp` DESC LIMIT 2) AS temp
ORDER BY RAND() LIMIT 1

Mysql ORDER BY using date data row

I have a query something like this:
SELECT
title, desc, date
FROM
tablename
ORDER BY
date ASC, title ASC;
Works fine when the data actually has a date. Issue is, date submission is optional, so I sometimes get 0000-00-00 as a date, which has the unfortunate effect of placing all nondated rows on top.
So, I then tried this:
SELECT
title, desc, date
FROM
tablename
ORDER BY
date DESC, title ASC;
Which sort of works, but not really -- all items with dates (non 0000-00-00) get listed in descending order, followed by all items with 0000-00-00.
What I want to do is order by date ASC, title ASC, but only if the date != 0000-00-00, but if date is = 0000-00-00, then just ORDER BY title ASC on those (I think I explained that correctly).
The only ways I can think to do this are non-SQL based (either 2 queries, or, each query just populates an in-memory array, and then I sort using PHP).
Is there a SQL query that can do this?
ORDER BY date = '0000-00-00' ASC, date ASC, title ASC
Your 2 query solution is a good one, you can do it all in SQL using the UNION command.
The first query will be for dates that are non-zero, then UNION in the query for dates that are zero.
Edit: Something like:
SELECT * FROM tbl
WHERE DATE != '0000-00-00'
ORDER BY date ASC
UNION SELECT * FROM tbl
WHERE DATE = '0000-00-00'
ORDER BY title ASC
This may not be very useful in this instance, but for complex queries, UNION can come in handy.

Categories