I have this SQL Query:
SELECT
*,
COUNT(*) as count
from tickets
where
status = :status
and DATE(closed_timestamp) = '".date("Y-m-d")."'
group by assigned_to
order by count(*) DESC
LIMIT 3
but i want to remove the closed_timestamp column from the tickets table and run the query based on the datetime column in the ticket_updates table
ticketnumber in the tickets table is equal to the ticketnumber (multiple rows) in the ticket_updates table
so it should run the above query without the closed_timestamp column and base it on the latest datetime value in the ticket_updates table
EXAMPLE
there are 2 rows in the tickets table where ticketnumber = 1234 and 5678
there are 3 rows in the ticket_updates table.. two rows have ticketnumber = 1234 and the other has ticketnumber = 5678
i want to show all the rows from tickets where status = 'Completed' and the last update (ticket_updates table) equals the current date (2014-05-19)
Use join query like below,
SELECT
t.*,
COUNT(t.*) as count
from tickets as t
LEFT JOIN ticket_updates as tu
ON tu.ticketnumber = t.ticketnumber
where
t.status = :status
group by t.assigned_to
order by count(t.*) DESC
LIMIT 3
try this:
SELECT *
FROM tickets t
JOIN (
SELECT ticketnumber
FROM ticket_updates
WHERE DATE(datetime) = CURDATE()
) u
ON t.ticketnumber = u.ticketnumber
WHERE status = 'Completed'
Related
SELECT *
FROM student as std LEFT JOIN
(SELECT * FROM billing WHERE `paid` < '4' ) AS bill
ON bill.reg_id = std.reg_id
GROUP BY bill.reg_id
ORDER BY bill.id
Here student table is main table and its primary key is foreign key in billing table.
There are multiple records in billing table with student reg_id .
I want latest billing record with limit 1
and no limit on student table.
Try This Query, I think it should work
SELECT *
FROM student as std LEFT JOIN
(SELECT * FROM billing WHERE `paid` < '4' group by reg_id order by id desc)
AS bill
ON bill.reg_id = std.reg_id
I'm assuming there's a column called billing_date, you'll have to update query with the correct timestamp. This is giving each billing record a rank based on its recency then pulling back only the most recent. It's assuming mysql 8.0, you can mimic this in earlier version.
select
*
from student std
left join (select
*, row_number() over (partition by reg_id order by billing_date desc) rn
from billing) bill on bill.reg_id = std.reg_id and rn=1
I have a table called bids, that have multiple rows.
these all have unique IDs however they have a listingID as well, so everytime it inserts it inserts a new row but with that listingID.
I'm trying to only return one unique result for the ListingID as appose to all the bids in the table , I tried SELECT DISTINCT and group by, but both didn't seem to work.
At the moment this is printing all the records from the table 'bids'
I would like to only print the last record for the listingID column.
$bids = $this->db->query("SELECT bidID,listingID, listing_title, bid_date, username,amount, starting_, sold, vintage, bottles, size, cases, sold_date, bid_type,
FORMAT(`bin`, 0) AS `bin`,
(CASE
WHEN ( SELECT COUNT(*) FROM bids WHERE bid_listing = listingID )
THEN
(SELECT FORMAT(amount,0) FROM bids WHERE bid_listing = listingID ORDER BY bidID DESC LIMIT 1)
ELSE
FORMAT(`starting_`, 0)
END
) AS `starting`
FROM (`bids`)
JOIN listings ON listingID = bid_listing
JOIN users ON list_uID = userID
WHERE bidder_ID = $userID
ORDER BY bidID DESC");
Try this sql. I used b1.* because I don't know what fields you're trying to return.
$bids = $this->db->query("select b1.* from bids b1
left join bids b2 on (b1.listingID = b2.listingID and b1.bidID < b2.bidID
where b2.bidID is null");
I have my table structure like below in desc order
sr group name status
4 class11 ghanshyam Joined
3 class11 ghanshyam Removed
2 class11 ghanshyam suspended
1 class11 ghanshyam joined
Now I want to fetch all record in group class11 whose status is Removed.
So my query will be
select * from table where group = 'class11' and status='Removed' group by name
but now ghanshyam has rejoined group after status removed and we are duplicating entries so it should not count in Removed, same for suspended account.
So how Can I fetch desired row from table
SELECT t1.* from [table] t1
INNER JOIN
( SELECT MAX(sr) AS nsr ,[group] ,[name] FROM [table] GROUP BY [group] ,[name] ) t2
ON t2.nsr = t1.sr
AND t1.status='Removed'
You should retrieve the record in all cases, like this :
select * from table where group = 'class11'
after doing that you can check the last status if its removed or joined.
Try this:
SELECT * FROM yourTable
WHERE group = 'class11' AND status ='Removed'
AND (SELECT MAX(sr) FROM yourTable WHERE group = 'class11' AND status ='Joined') < sr
GROUP BY name
Hope that this will help you :)
Rest for any clarification, please add comment.
Try this query it will get records which have maximum id for removed
SELECT * FROM tbl_name t WHERE t.sr IN
(SELECT max(t1.sr) FROM tbl_name t1 WHERE `group` = 'class11' GROUP BY t1.`name`)
AND t.status = 'Removed'
Check thus fiddle:
http://sqlfiddle.com/#!9/ef189a/1
I have a table as follow. I want to run a query to select the data from the table using php in which if date column is repeated then I want to take only last row of that date
id Date start end publish
1 04-Nov-2015 1000 1300 0
4 04-Nov-2015 2100 3500 0
5 05-Nov-2015 1500 3000 0
like for the below table, When I run the query then the result should come:
4 04-Nov-2015 2100 3500 0
5 05-Nov-2015 1500 3000 0
When I run the query
$select = mysql_query("select * from `entry` Group by `date`") or die(mysql_error());
Then It shows the first row of repeating table, What should I modify in the query that the result should show the last row of repeating colum
Select * from (Select * from entry order by date,id desc) x group by x.date
You can do this with this approach:
$select = mysql_query("select * from `entry` Group by `date`" ORDER BY id DESC LIMIT 1) or die(mysql_error());
Try inner query, I'm not sure following will work exactly as I cant test that now, but for getting result you have to use inner query. Inner query help me to get expected result in my case.
SELECT *
FROM entry p
WHERE id =
(SELECT max(id) FROM entry p2
WHERE p2.id = p.id)
GROUP BY p.date
ORDER BY p.id DESC;
This query will work for you:
create TABLE test (id INT PRIMARY KEY, tdate DATE, start INT);
SELECT t1.* FROM test as t1
LEFT JOIN test as t2
ON (t1.tdate = t2.tdate AND t1.id < t2.id)
WHERE t2.id IS NULL;
Try this query :-
select * from( select * from entry order by id DESC ) new Group by date
My tables are like this:
Table 1 (students)
Table 2 (results)
I want to select all students from Table 1 students who have 4 results in the results table. I tried this query, but with no success:
SELECT *
FROM students
WHERE gender = 'm'
AND (SELECT COUNT( result ) AS count
FROM results
INNER JOIN students ON results.stuID = students.stuID
WHERE result !=0
) =4
ORDER BY rank ASC
You can rewrite your query by using join and HAVING clause to check the count for each student group ,This can be done without using the subquery which sometimes affects on performance
SELECT s.*,COUNT(*) AS count
FROM students s
INNER JOIN results r ON r.stuID = s.stuID
WHERE r.result !=0
GROUP BY s.stuID
HAVING count =4
ORDER BY s.rank ASC
um, that's a little convoluted.
the where clause should come after the subquery, and the subquery still needs to be JOINed back to the main query.
something like
SELECT * FROM students
INNER JOIN (SELECT COUNT(result),results.stuID as count FROM results WHERE result != 0) as result_count
ON result_count.stuID = students.stuID
WHERE result_count.count =4 AND students.gender = 'm'
ORDER BY rank ASC
You have to use alias for table also -
SELECT *
FROM students as a
WHERE gender = 'm'
AND (SELECT COUNT(result) AS count
FROM results as b
WHERE b.stuID = a.stuID AND
(result!=0 OR result IS NOT NULL OR result!='')
) = 4
ORDER BY rank ASC