简体   繁体   中英

select data from two mysql tables using PHP

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM