Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL, UNION TABLES

Tags:

mysql

union

Got a query like this:

SELECT * FROM job WHERE status!=2 AND status!=3
UNION SELECT * FROM emp WHERE status!=2 AND status!=3
ORDER BY (id/popularity) DESC LIMIT {$from},$vpc

It works perfectly, but now can't identify where from record is coming... Is there any way to identify from which table, the record is coming?

like image 894
simultsop Avatar asked Sep 18 '26 11:09

simultsop


1 Answers

I guess you could try this:

      SELECT *, 'job' as origin 
        FROM job 
       WHERE status!=2 
         AND status!=3
UNION SELECT *, 'emp' as origin
        FROM emp 
       WHERE status!=2 
         AND status!=3
    ORDER BY (id/popularity) DESC 
       LIMIT {$from},$vpc

This should add a field named origin to your results, containing a name of your choice to identify the table.

like image 109
greg0ire Avatar answered Sep 22 '26 14:09

greg0ire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!