我该如何过滤此表?

时间:2013-02-21 13:23:19

标签: php mysql

我应该在这个sql中添加什么? 在我的系统中,我有外键ID_Number。我希望只有一个ID_Number必须显示在具有相同ID_Numbers的所有ID_Number中。并选择所有没有0或Bill.Arrears521没有0的Bill.Arrears125和Bill.Pay_Date必须是最新的。

我只需要显示所有ID_Number,其中Arrears125和Arrears521不等于0且Pay_Date是最新的

 __________________________________________________________
|  ID   | Arrears125 | Arrears521|  Pay_Date  |  ID_Number |
 ----------------------------------------------------------
|   1   |      0     |     0     |  12-01-01  |      1     |
 ----------------------------------------------------------
|   2   |      0     |     0     |  12-01-02  |      1     |
 ----------------------------------------------------------
|   3   |      25    |     25    |  12-01-03  |      1     |
 ----------------------------------------------------------
|   4   |      0     |     0     |  12-01-01  |      2     |
 ----------------------------------------------------------
|   5   |      0     |     0     |  12-01-02  |      2     |
 ----------------------------------------------------------
|   6   |      0     |     0     |  12-01-01  |      3     |
 ----------------------------------------------------------
|   7   |      25    |     25    |  12-01-02  |      3     |
 ----------------------------------------------------------

仅查看ID 3和ID 7

  $sql=mysql_query("SELECT
                        Bill.*,users.* from Bill,users
                    WHERE
                        users.ID_Number=Bill.ID_Number
                    AND
                        (Bill.Arrears125!=0 OR Bill.Arrears521!=0)
                    GROUP BY
                        Bill.ID_Number")or die(mysql_error());

1 个答案:

答案 0 :(得分:0)

select str_to_date(Pay_Date,"%Y-%m-%d") as date_temp, ID_number from Bill where Appears125 != 0 and Appears521 != order by date_temp desc