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
Related
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
I want to display the logs to recent activities page ordered by date. Now I was trying to execute this to my mysql
"SELECT * FROM tracking_log.editlog, tracking_log.deletelog, tracking_log.loginlog, tracking_log.logoutlog ORDER BY time ASC";
but it always says
Column 'time' in order clause is ambiguous
all of the tables have a time column, format by datetime (0000-00-00 00:00:00)
How am I going to fetch them ordered by time?
Thanks in advance!
By which table's time column you want to order?
Assuming you want to order the result set by tracking_log.editlog.time column then the query would look like below:
SELECT
*
FROM tracking_log.editlog, tracking_log.deletelog,
tracking_log.loginlog, tracking_log.logoutlog
ORDER BY tracking_log.editlog.time ASC;
Just in case if all of the time columns in the respective table don't contain NOT NULL values at the same time then you need to use COALESCE I guess.
Query using COALESCE
SELECT
*
FROM tracking_log.editlog, tracking_log.deletelog,
tracking_log.loginlog, tracking_log.logoutlog
ORDER BY
COALESCE(tracking_log.editlog.time , tracking_log.deletelog.time, tracking_log.loginlog.time,tracking_log.logoutlog.time) ASC;
'tracking_log' is your database name, and you're selecting multiple tables from that database, so you need to specify from which table you want to order 'time' by:
select * from tracking_log.editlog, tracking_log.deletelog ORDER BY tracking_log.editlog.time ASC
or whichever table from your database you want to use 'time' from. This will fix the error but won't return any results because you have multiple tables in a SELECT clause without anything relating them together.
You'll need to specify some common columns on which you want to return results rather than getting the wildcard and then UNION the tables to aggregate the results. For example, if you have common columns userID, description and time in all your tables, you could do the following:
(SELECT userID, description, time FROM tracking_log.editlog)
UNION
(SELECT userID, description, time FROM tracking_log.deletelog)
ORDER BY time
I have a question about MySQL. I have a Table with this fields:
WorkerName
Date
HoursWorked
Ok, if I do this Query:
SELECT WorkerName, Date, HoursWorked, SUM(HoursWorked) FROM myTable GROUP BY WorkerName
I have the field grouped by the Worker Name BUT with a only row. I want to show all days worked by this Worker in the same row, and the other Worker in another row.
In PHP actually have a While that shows all days worked, but only shows the Hours of the first day sorted.
You can use GROUP_CONCAT aggregate function:
SELECT
WorkerName,
GROUP_CONCAT(Date) AS dates_worked,
SUM(HoursWorked)
FROM
myTable
GROUP BY
WorkerName
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()
First sorry for the long question title.
My question/situation is as such.
1.) I have 2 tables in mysql
2.) In first table, each listing has a unique id(each listing is in 1 row)
3.) In the second table it has the name/tags for images linked to the listing id,from the first table
4.) Each listing can have multiple images(multiple row in the second table).
What i am trying to do is to pull all the listings from table 1 and then use the listing.id from table one to pull all the rows of images from table 2 that are linked to the listing.id.
I am confused at the moment because there are multiple rows that has the same listing.id from table 2. ANd i tried query to display* but it only echo the last image(row) from table 2.
It doesnt seem to work when i join the 2 tables. And i am not sure if i query it twice then push array together.
Thanks for your time
$result=mysqli_query($con,"SELECT * FROM Listing JOIN listingpic ON
(Listing.id = listingpic.listingid)
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+300 DAY GROUP BY Listing.id ORDER BY Listing.id DESC") or die( mysqli_error($con));
while ($row = mysqli_fetch_assoc($result))
{
$output[] = $row;
}
if (!empty($output)){
echo json_encode( $output );}
else{
echo json_encode( [] );
}
You only get one result per id, because you used GROUP BY Listing.id. If you do not group them, you get one result row for each table 2 row, including the Listing id each time.
If you wish to retrieve the data in one query in the form "one id: multiple data", you can use GROUP_CONCAT for example and then explode() the retrieved string result.
Otherwise get all ids from table 1 and then iterate over them in PHP and do one additional query per ID
Pro tip: Don't use the viciously confusing MySQL extension to GROUP BY. Read this: http://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html
Pro tip: Don't use SELECT *, especially when you're joining tables. Instead, enumerate the columns you want in your result set.
Inherent to SQL is the idea that resultsets, like tables, are rectangular. They have rows and columns. Each row usually represents some real world item -- an "entity" -- and each column represents some attribute of that entity.
The result set you describe will, inherently, repeat information from your first table so it can show the info from the second table row by row.
What you want for a query is this, I think.
SELECT Listing.id, listing.date,
listingpic.id, listingpic.url, listingpic.caption
FROM Listing
JOIN listingpic ON Listing.id = listingpic.listingid
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+300 DAY
ORDER BY Listing.id DESC, listingpic.id
This will give you one row per image.
If you're running out of memory it's because your result set is massive. You may want to limit it somehow, either using a first and last publication date:
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+300 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())+293 DAY
or with a LIMIT clause.
ORDER BY Listing.id DESC, listingpic.id
LIMIT 100