Union Query in Mysql

by admin on February 20th, 2012

$query = “(
SELECT DATE_FORMAT( Date, ‘%m/%d/%Y’ ) AS date_time, id, type, ‘notes’ as table_name
FROM notes
WHERE p_id ='”.$pid.”‘
)
UNION (
SELECT DATE_FORMAT( DOS, ‘%m/%d/%Y’ ) AS date_time, id, type, ‘mt_d’ as table_name
FROM mt_d
WHERE p_id ='”.$pid.”‘
)
UNION (
SELECT DATE_FORMAT( created, ‘%m/%d/%Y’ ) AS date_time, id, type, ‘docs’ as table_name
FROM docs
WHERE p_id ='”.$pid.”‘
)
ORDER BY date_time DESC
limit “.$_POST[‘start’].’,’.$_POST[‘limit’];

And the reslut is:

Code:
{“notes_list”:[{“date_time”:”07\/13\/2010″,”id”:”9″,”type”:”Chart Note”,”table_name”:”notes”},{“date_time”:”07\/13\/2010″,”id”:”10″,”type”:”Chart Note”,”table_name”:”notes”},{“date_time”:”07\/13\/2010″,”id”:”3″,”type”:”Chart Note”,”table_name”:”notes”}],”totalRows”:12}

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>