php - how to count the mysql data when match rules -


i show how many promotion code left function. find out how query latest promotion. show code how many left.

now have 2 table,

this t1.

+--------------+--------------------------+----------------------+ |    id        |  name                    | description          | +--------------+--------------------------+----------------------+ | 1            | gg                       | gg           | | 2            | abc defg                 | abc ddddd            | | 3            | ccard                    | gooooo               | +--------------+--------------------------+----------------------+ 

this t2

+---------+------------+-------------------+------------------+ | id      | kaid       | code              | timestamp        | +---------+------------+-------------------+------------------+ | 1       | 2          | zzzzaaaaa         | 123456789        | | 2       | 2          | aaazzadwww        | 123344444        | | 3       | 1          | asfasdffff        | 123333333        | | 4       | 2          | hhhhhfdfg         | 123222222        | | 5       | 1          | asdasdadddd       | 123111111        | | 6       | 1          | aaaaaaaa          |                  | | 7       | 1          | dgdfgdsfg         |                  | +---------+------------+-------------------+------------------+ 

i query latest user promotion code this

    $querylist = mysql_query("select t2.*,t1.name t2,t1 t1.id = t2.kaid  order t2.timestamp desc limit 5"); while($rowlist = mysql_fetch_row($querylist)) {     $idlist[] = $rowlist['id'];     $user_list_latest[] = $rowlist; } 

after loop query, data this

1. gg 2. abc defg 3. gg 4. abc defg 5. abc defg 

then show count this. hypothesis count data $countleft

1. gg  (this promotion code 2 left!) 2. abc defg (this promotion code 0 left!) 3. gg (this promotion code 2 left!) 4. abc defg (this promotion code 0 left!) 5. abc defg (this promotion code 0 left!) 

the number 2 , 0 $countleft. t2 timestamp means user valid promotion code time. timestamp empty means nobody yet.

change query , try

select name,balance t1 join (select kaid,count(id) balance t2  isnull(timestamp) group kaid)t3 on (kaid=t1.id) order balance desc limit 5; 

in loop print every row , column(there 2 column each row). give answer.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -