user3223205
user3223205

Reputation:

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)

Upvotes: 1

Views: 91

Answers (2)

downforme
downforme

Reputation: 413

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'

Upvotes: 0

Manibharathi
Manibharathi

Reputation: 945

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

Upvotes: 0

Related Questions