So, I have a database table holding tickets. One of the fields is "availableUntil".
when I make the database call to get tickets, I want to be able to exclude those tickets who's date has already been passed relative to the current date.
Anyone know of a way to do this?
SELECT blablabla... WHERE availableUntil >= CURRENT_TIMESTAMP
SELECT [what_you_want] FROM your_base WHERE availableUntil > CURRENT_DATE();
Related
I am having trouble trying to come up with a specific mysql query. I haven't even found any reference close to what I am looking for. I am not sure if I am searching the right questions...
I have a database that stores a list of unique ids. The ids then get unique entries in the database whenever they do something. So it isn't unusual for one id to appear hundreds of times each month in my database. I have about 5 years worth of ids currently in the database. The query I am looking to build is to identify the IDs that are new since the beginning of June.
So far the only queries that I have been able to come up with all specify specific ids in June, where I want the ids that first appeared in June and never before.
Any help would be greatly appreciated.
Edit:
I have a timestamp of when every entry was added to the table
The following should work:
SELECT ID FROM (
SELECT DISTINCT(ID) as ID
FROM YOUR_TABLE
WHERE DATE >= "2018/06/01") b
WHERE b.ID NOT IN (
SELECT DISTINCT(ID) as ID
FROM YOUR_TABLE
WHERE DATE < "2018/06/01")
It creates a selection of id's before and after june, then compares them for new ones after june. An easier way to do it is just set a first action column on the user to denote when they became "active" though.
what I have is a single database with multiple tables on a server that I want to run a query against to find the values of any fields, in any table, that have a date greater than today's (current day) date.
Any answer would be greatly appreciated and thank you in advance.
For a single table the query is >>
SELECT * FROM db_name.`table_name` WHERE date_column_name >= CURRENT_DATE;
Now for running it on multipe tables you will need to use JOINS. According to the structure of result and usage you can use any of the joins. Read about them here. You can just add the Join clause in the query above.
I have a table lead and there is a field called added_on (datatype timestamp), I want to to fetch only the leads which are interested in a particular product and the reports should come monthly.
interested_in is a field in the lead table where the interested product's id will be stored as a comma separated values.
and $prod_id is stored with a product id which has to be checked.
the below query works fine just to fetch out the leads which are interested in a particular product. but i want the results to come month by month.
select*from lead where find_in_set('$prod_id',interested_in)
Please guide me what i have to do to achieve that
TRY
WHERE MONTH(added_on) = $giveMonthNumber
OR
WHERE MONTHNAME(added_on) = $givenMonthName;
Reference :
MySQL date time functions
Do this:
select * from lead where find_in_set('$prod_id',interested_in) group by added_on
Is it better to store dates in mysql in three columns or use just one column. Which one is faster. Also, if I just want to do inserts with todays date in format dd/mm/yy , how do I do that. and also how do I do selects with that. Also, lets say if I wanted to get results for all the wednesdays, how do I do that or lets say one date 25th of all the months and years, how do i do that.
Thanks People.
I am using PHP with Apache and Mysql.
What are the drawbacks of using the structure that I am proposing. I can easily get all the 25th by using the date table and I can get all the days using another column for days. How much difference would be there in the terms of speed between my proposed solution and using a DATE table?
You will want to use a proper column type, such as DATE, DATETIME, or TIMESTAMP, depending on your needs. They are built specifically to handle dates, and can more easily perform other functions (adding, comparing, etc.) that would be difficult to perform on 3 separate columns.
Read this for more info.
DAYOFWEEK(date) will give you a numeric representation for the day. In your case, 4 = Wednesday. DAYOFMONTH(date) will work for finding all 25th days of the month.
DAYNAME(date) will return the name of the weekday for date
Also, if I just want to do inserts with todays date in format dd/mm/yy ,how do I do that.
Well it depends on the format your date is passed in through your
form but you are going to want to store your date in YYYY-mm-dd format.
INSERT INTO my_table (timefieldname) VALUES ( '$date' );
and also how do I do selects with that.
SELECT timefieldname FROM my_table;
//or you can format the date - this will give you month/day/year 01/01/2012
SELECT DATE_FORMAT(timefieldname, '%m/%d/%Y') FROM my_table;
Also, lets say if I wanted to get results for all the wednesdays,
SELECT timefieldname FROM my_table WHERE DAYNAME(timefieldname) = 'Wednesday';
How do I do that or lets say one date 25th of all the months and years, how do i do that.
SELECT timefieldname FROM my_table WHERE DAY(timefieldname) = '25';
You can free up having to pass dates from your codebase and let mysql insert them for you, provided they are time stamps:
ALTER TABLE tablename ADD `timefieldname` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ;
It's not much of a speed boost, but it does reduce your need to code and validate variables passed to the database.
So, the query I'm trying to make looks like this right now:
SELECT `jid`, MAX(`start`) as `start` FROM `dates` WHERE `start` < NOW() GROUP BY `jid`
The table dates basically holds a DATETIME value start associated with a job.
As you probably guessed, jid is the id of a job, stored in another table.
A job can have many dates
What I'm trying to accomplish is to find out what jobs have all their start dates in the past.
My thought was to select the highest values of the start dates, grouped by jid and if they were in the past, it would imply that all other dates associated with the same job are also in the past.
This query isn't working because it's checking for start dates in the past and then selecting the highest of those. This doesn't exclude the possibility of another date for the same job, lying in the future.
I have no idea how I could proceed now. Any help or clue is appreciated.
Cheers - Tom
You have to use HAVING :
SELECT jid, MAX(start) as start
FROM dates
GROUP BY jid
HAVING MAX(start) < NOW();
HAVING acts a bit like WHERE. It filters out the rows after they were selected. Usually (and actually, I can't think of any other case), you only use HAVING with aggregate functions.
(I hope you really inserted dates in the future in your table, though!)