mysql - Different Value from first select and second select (UNION WITH GROUP BY) -


i have table users:

user_id | ui_id --------+-------    4    |   16 --------+-------    5    |   17 --------+-------    9    |   21 

user_info:

ui_id |      fname      |     lname ------+-----------------+--------------   16  |     joanalyn    |    lalicon ------+-----------------+--------------   17  |   jose allan    |   dela cruz ------+-----------------+--------------   21  |       steve     |   dela cruz 

overtime:

ot_id | approve_by ------+------------   3   |     4 ------+------------   6   |     9 ------+------------   8   |     5 ------+------------   9   |     9 ------+------------   16  |     4 

and loa:

loa_id| approve_by ------+------------   4   |     9 ------+------------   6   |     4 

i want full name, qty, , qty2 field in 1 query. can't working. can sum of ot_id count , loa_id count can't value separated.

my query:

select name,qty2 <---- value second select (select concat(ui.fname,' ',ui.lname) name,         count(o.ot_id) qty overtime o inner join users u on o.approve_by=u.user_id inner join user_info ui on u.ui_id=ui.ui_id group ui.ui_id  union  select concat(ui.fname,' ',ui.lname) name,         count(l.loa_id) qty2 <----- can't value loa l inner join users u on l.approve_by=u.user_id inner join user_info ui on u.ui_id=ui.ui_id group ui.ui_id) ui  group name 

i can't qty2 qty working. , if select sum(qty) sum ot_id , loa_id

i want this:

       name          |        qty       |       qty2     ---------------------+------------------+-------------------   joanalyn lalicon   |        2         |         1 ---------------------+------------------+-------------------  jose allan dela cruz|        1         |         0 ---------------------+------------------+-------------------    steve dela cruz   |        2         |         1 

try this:

select concat(ui.fname, ' ', ui.lname) name      , count(o.ot_id) qty      , (select count(*) loa approve_by = u.user_id) qty2   users u   join user_info ui on ui.ui_id = u.ui_id   left join overtime o on o.approve_by = u.user_id  group u.user_id; 

sql fiddle


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -