Mysql Select data and time - php

Well i writing betting script got all data in mysql
Table Games row BetillDate row BetillTime
BetillDate| BetillTime
--------------------
2015-01-21|15:00:00|
2015-01-29|15:00:00|
2015-01-27|15:00:00|
How to Mysql Select Date its today and time its now in one select...
This select not working right ..
SELECT DISTINCT
class.`name`
FROM
class
INNER JOIN market ON class.game_id = market.class_id
WHERE
market.betTillDate > NOW() AND
market.betTillTime > NOW()
or if its not possible how can i do it?
Thanks!

I suggest fixing the database by adding a field of DATETIME type and concatenating those two fields into it.
Alter table TABLEX add BetillDateTime DATETIME;
Update TABLEX set BetillDateTime = TIMESTAMP(CONCAT(BetillDate,' ',BetillTime));
Now you have a single field for both date and time, and should fix your code to use only that one, then later drop the other two.
And now all you need in the where clause is:
WHERE market.BetillDateTime > NOW()

SELECT DISTINCT
class.`name`
FROM
class
INNER JOIN market ON class.game_id = market.class_id
WHERE
TIMESTAMP(market.betTillDate,market.betTillTime) > NOW()

Related

Mysql get max row and min row from group of each row

I have MySQL table with employees attendance. first row of a day of employee treating as in time and last row of a day of employee treating as out time. I am trying to select first and last (min time and max time) from attendance table. It should give me two row sets. but my query not giving me as i expecting the result.
Table (Attendance)
My Query
select *, min(attdate) as intime, max(attdate) as outtime from attendance where empid=1
But above query not giving me as expected result. My output should be in below image. Please suggest me the query or give me hint to achieve given output.
this can be done by sub queries in where conditions.
SELECT * FROM attendance AS c WHERE empid=1 and (
attdate=( select min(attdate) from attendance where attendance.empid=c.empid )
or attdate=( select max(attdate) from attendance where attendance.empid=c.empid )
);
Unfortunately, MySQL doesn't offer window functions, so it's a bit more difficult here. You can use exists :
Select * from yourtable t
Where not exists (select 1 from yourtable s
Where t.empid = s.empid and
(s.attndate < t.attndate or s.attndate > t.attndate))
Though it seems you need to add another condition t.date = s.date unless you have only 1 day records stored there

Duration between type in and out in MYSQL

I am new MYSQL now i try something here is my query not accurate result
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time))) FROM `officialbreaks` where type='out'
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time))) FROM `officialbreaks` where type='in'
Any buddy have work on that type of situation.
OUT REQUIRED
TIMEDIFF(time where type=in, time where type=out)
3:35:30 time type=in, 03:35:30 time type=out
Output 0
You can try as per below-
SELECT DISTINCT a.userid, (TIME_TO_SEC(b.time) - TIME_TO_SEC(a.time)) AS 'time_diffrence' FROM
(SELECT userid,`time` FROM mytable WHERE `type`='in') a
JOIN (SELECT userid,`time` FROM mytable WHERE `type`='out') b ON a.userid=b.userid
It is a simple solution but there can be multiple out time against single in time and multile users etc. so there can be so many combination where query need to change.

Work out timestamps / other data based on two MySQL tables

I have two tables in my database:
tickets
ticket_updates
each table has a column called ticketnumber which match. there are sometimes multiple rows in ticket_updates where there is only one row in tickets
I want to be able to show the number of rows from tickets where status = 'Completed' but where it has been completed TODAY
for each row in the ticket_updates table there is a datetime column
As there are multiple rows in ticket_updates for each 1 row in tickets it will need to select the latest datetime from ticket_updates too
You should be able to do this with a simple join and a little MySQL date function:
select
count(sub.counter)
from
(
select distinct
ti.ticketnumber as counter
from
tickets ti
join ticket_updates tu
on ti.ticketnumber=tu.ticketnumber
where
ti.status='Completed'
and date(tu.datetime)=curdate()
) sub
If your datetime (Assuming that isn't the actual name) contains date AND time information, you will need to strip out the time component to compare it properly to the value returned by curdate() which is just a date of today.
The MySQL date() function returns just the date component of a date and time.
Edit: Corrected code to account for multiple relationship as correctly pointed out by #mituw16
I am going to assume there is a key linking these two tables. You might try something like this...
select count(*) as TicketCount from tickets
join ticket_updates on ticket_updates.ticketnumber = tickets.ticketnumber
where tickets.status='Completed' and ticket_updates.datetime = CURDATE()
group by ticket_updates.ticketnumber

Get unique values of SQL table and echo with PHP

I have a MySQL table with over 200 values. One of the columns on my table is 'date'. Out of all 200 values there are only 5 unique dates.
How can I list out the unique values of the dates and echo them with php. e.g. not getting back 200 instances of dates but just 5.
Use DISTINCT
SELECT DISTINCT `date` FROM `tablename`....
SELECT myDate FROM myTable GROUP BY myDate
Or...
SELECT DISTINCT myDate FROM myTable
DISTINCT is a nice short hand, but if you ever then want to make use of the query for other purposes, if often constrains you a bit too much. So I prefer the GROUP BY version.

MySQL select between two dates not working as expected

I'm trying to create a query that will select all dates between two dates
This is my query:
$query = "SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN '$date1' AND '$date2' AND D1.D1_ID = D2.D2_ID";
The trouble is, it is not returning anything, but not producing an error either
So I tried inputting it directly into phpMyAdmin like this
SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN '2011-01-01' AND '2011-12-12'
AND D1.D1_ID = D2.D2_ID`
then like this
SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN '2011-01-01' AND '2011-12-12'
and like this
SELECT * FROM D1
WHERE DATE_ADDED BETWEEN '2011-01-01' AND '2011-12-12'
and I just get
MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0003 sec )
Yes, my tables exist, and so do the columns :)
In the first cases the lack of results could be because of the inner join. For a result to be in the set it would require a record in both tables, ie. a record from d1 would not appear unless d2 also had that id in the d2_id column. To resolve this, if that is correct for your business logic, use left join.
However, the last of your cases (without the join) suggests the reasons is a lack of matching records in the first (left) table d1.
Without the full dataset we can't really comment further, since all the code you are running is perfectly valid.
If you always want to select an entire year it is easer to select it like this:
SELECT * FROM D1 WHERE YEAR(DATE_ADDED) = 2011;
Please implement below code
SELECT DISTINCT * FROM D1,D2
WHERE D1.DATE_ADDED BETWEEN DATE_FORMAT('2011-01-01','%Y-%m-%d')
AND DATE_FORMAT('2011-12-12','%Y-%m-%d')
AND D1.D1_ID = D2.D2_ID`

Categories