php - mysql group and filter results -


i have 2 queries!

select line, account datas date between "2015-07-01" , "2015-07-15"  , sig="in" group account order account 

and

select line, account datas date between "2015-07-01" , "2015-07-15"  , sig="out" group account order account 

sample results:

line-account, sig 0-1309  in 0-1331  in 0-1340  in 0-1361  in 0-1404  in 0-1555  in  line-account, sig 0-1098  out 0-1309  out 0-1322  out 0-1331  out 0-1340  out 0-1555  out 

i want 2 queries give results:

  1. give me accounts have "in" sign, hasn't "out" sign!
  2. give me accounts have "out" sign, hasn't "in" sign!

i solved in excel, complicated solve day day! ;)

if easier accept solution in php too!

i hope understand want!

thanks in advance

have tried this?

select *  (     (select line, account, sig     datas     date between "2015-07-01" , "2015-07-15"      , sig="in"     group account     order account)     union     (select line, account, sig     datas     date between "2015-07-01" , "2015-07-15"      , sig="out"     group account     order account) ) t1 group line, account having count(line) = 1 

edit:corrected query

edit: here's brief explanation on how query works. first off, select every line has sig = 'in'... append every line has sig = 'out'.

now gets interesting. if line has both in , out statement, it'll appear twice in our current resultset because it'll part of both sig = "in" , sig = "out" query. on other hand, line has "in" or "out" appear in 1 of 2 inner query. we're going take advantage of this.

the outer select statement "group line, account" statement flattens our resultset , allows use count() function. if line appears in both queries, count() equal two... on other hand, if line appears in 1 of 2 queries, count() equal one. lines want.

still, in sql, can't do:

select * table count() > 1 

if want place condition using aggregate function, have use "having" clause, placed after "group by" statement , acts "where", aggregate function.


Comments