mysql select ID where date is current date - php

i have this table structure:
Rows
ID | Counter | Dates
Values
1 | 100;300;44 | 01.01.2016;02.11.2016;03.03.2017
each ID is connected to an separated user.
at the moment i have 100 users, so 100 data in my table.
now i need an sql command, which show me the the ID from the user, which have a date in the Dates-Row, which is the same like the current date

This is untested, but it should be:
SELECT * FROM `table` WHERE Dates LIKE CONCAT('%',DATE_FORMAT(CURDATE(),'%m.%d.%Y'),'%');
This will select the current date CURDATE(), put it in the format you use in your table, then stick wildcards on each end so it will look inside of the string within your Dates column.

Related

get data between start date and end date

Get data between start date and end date
I am getting data but my requirement is little bit different pls check the below database
I have two text boxs
1) start date
2) end date
Database is like this
table name->purchase_order
sno start_date end_date
1 2017/08/01 2017/12/01
2 2017/08/01 2017/11/30
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
5 2017/10/01 2017/11/30
I am trying like this
select *
from purchase_order
where start_date>= '2017/09/01' and start_date<= '2017/09/01'
OR end_date>= '2017/09/01' and end_date<= '2017/09/01'
Output i am getting
sno start_date end_date
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
What i require
if i select between this 2017/09/01 - 2017/09/30 i want out put like this {in id "1" in between 8th month to 12th month there is 9th month is there so it also has to come}
sno start_date end_date
1 2017/08/01 2017/12/01
2 2017/08/01 2017/11/30
3 2017/09/01 2017/09/30
4 2017/09/01 2017/10/30
thanks
Instead of checking that the column values are between your dates, you want to see if your dates are between the column values. Using BETWEEN makes the logic look a little cleaner:
select * from purchase_order where '2017/09/01' BETWEEN start_date AND end_date
If you select 2017/09/01 - 2017/09/30, then do this:
select * from purchase_order where '2017/09/01' BETWEEN start_date AND end_date AND '2017/09/30' BETWEEN start_date AND end_date
AND will make sure both dates are between start_date and end_date, OR will make sure at least one of the dates is between the columns.
You would not want to have 4 clauses as you have in your question, you would want to have just 2.
The logic should be as follows:
the start date from the input in the application is later or equal to the start date in the start column in your table
the end date from the input in the application is earlier or equal to the end date in the end column in your
application.
Based on this logic, you should be able to use the following query:
SELECT
*
FROM purchase_order
WHERE start_date<= '2017/09/01' -- start date input
AND end_date<= '2017/09/01' -- end date input
One may complain that I have a hole in my logic due to the fact that I don't check to see that the end date is later than the start date, though you would want to do that at the application level, hence it is not relevant at the database level where this question was asked.

Get data by time range from Redis

I have MySQL table elements_info with fields:
element_id | price | time_start | time and
To get element_id and price fields from MySQL i use query:
SELECT * FROM `elements_info` WHERE 1447141365 BETWEEN time_start AND time_end;
Where 1447141365 - current timestamp.
Can I store this structure in Redis (maybe zset)?
Or maybe I should choose another NoSQL DB to get elements by time range?

count rows with same date

i got a table named date_use and has a column id & dateUSE.
id dateUSE
1 2015-01-01
2 2015-01-01
3 2015-01-01
4 2015-01-02
5 2015-01-02
now if i will submit another date 2015-01-01 to this table using my php code, it will first count the rows that has the same date and if it is equals to 3 then the date 2015-01-01 that i selected will not be saved and will not be available to be selected.
i want to compare first the date i submitted then if that date has already inputted 3 times in the database then the date i submitted will not be available to be saved and i will just select other date.
just like in checking a reservation date availability, i will select a date then click a button then it will check first my database to see if that date was entered three times then the date i selected will not be available.
You should do this:
SELECT
COUNT(*) AS Total
FROM tablenames
WHERE dateUSE = '$date';
Then in your php code, read this value for total if it is equal 3, then don't do the insert.
You need either a trigger for this or application level logic. Here is an example of application-level logic:
insert into t(date)
select '2015-01-01' as newdate
from (select count(*) as cnt
from t
where date = '2015-01-01'
) x
where x.cnt < 3;
You can try execute this in phpmyadmin if you have installed:
SELECT count(*) FROM date_use WHERE dateUSE='2015-01-02';
after this you will figure out how to implement this in script.

mysql select date using timestamp

in my database i use a column named startdate and in the column there are rows with timestamps, looking like: 1410178260
Normally, when i use a datetime field and i want to select all the items with the date of today, i run this query:
$sql = "SELECT id FROM agenda2 WHERE DATE(startdate) >= CURRENT_DATE()";
But now, using the timestamps, i don't know how to make a query that selects all the items inserted today.
Can someone help me with that?
You need to convert to date using the function from_unixtime()
mysql> select FROM_UNIXTIME('1410178260');
+-----------------------------+
| FROM_UNIXTIME('1410178260') |
+-----------------------------+
| 2014-09-08 17:41:00 |
+-----------------------------+
So you may do as
SELECT id FROM agenda2 WHERE DATE(FROM_UNIXTIME(startdate)) >= CURRENT_DATE()
If you're using unix timestamps then you've got to use
$sql = "SELECT id FROM agenda2 WHERE DATE(FROM_UNIXTIME(startdate)) >= CURRENT_DATE()";
Consider that both versions can't make use of an index.
see FROM_UNIXTIME

Mysql alternate week days

My table has the following columns
| customer_id | service_start_date |
I want to provide the service on alternate week days from the service start date (eg:- every other mondays, every other tuesdays etc..)
If the service_start_date is a monday, then the service will be delivered on every other mondays.
Is there any way to query the mysql table to get all customer_ids who needs service on a particular date?
Try something like
SELECT customer_id
FROM Table as t
WHERE MOD(DATEDIFF(DATE(NOW()), DATE(service_start_date)), 14) = 0
This is assuming that your service_start_date is always a weekday.
Addition to Clami219s answer:
To get the customer(s) that need service on a particular date (as of your request), use
SELECT customer_id
FROM table_name
WHERE
service_start_date <= NOW() AND
WEEKDAY(service_start_date) = WEEKDAY('2014-06-25'); //whatever date you like to fetch

Categories