如何使用php比较和查看mysql中的三个表?

时间:2015-07-30 12:31:34

标签: php mysql

我有三张桌子:

第1个月是

id  date        days
1   2015-06-01  Monday
2   2015-06-02  Tuesday
3   2015-06-03  Wednesday
4   2015-06-04  Thursday
5   2015-06-05  Friday
6   2015-06-06  Saturday
7   2015-06-07  Sunday

第二次出席

id  user_name   date    checkin
1   ali     2015-06-01  10:43:17
2   ali     2015-06-03  10:22:39
3   ali     2015-06-04  13:36:52
4   ali     2015-06-05  14:36:52

第3次出席

id  user_name   date    checkout
1   ali     2015-06-01  05:03:17
2   ali     2015-06-03  06:00:39
3   ali     2015-06-04  06:36:02
4   ali     2015-06-05  06:06:02

我想要这样的结果:

username  date      checkin   checkout  day

ali     2015-06-01  10:43:17  05:03:17  Monday
        2015-06-02                      Tuesday
ali     2015-06-03  10:22:39  06:00:39  Wednesday
ali     2015-06-04  13:36:52  06:36:02  Thursday
ali     2015-06-05  14:36:52  06:06:02  Friday
        2015-06-05                      Saturday
        2015-06-05                      Sunday

我该如何显示这样的结果?

我正在使用此查询:

SELECT * 
FROM attend 
WHERE user_name='ali' 
AND date BETWEEN '2015-06-01' AND '2015-06-07'  
Order by date

3 个答案:

答案 0 :(得分:0)

像这样的东西

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;


public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MEL_DEVConnectionString"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{
    con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
    SqlCommand cmd = new SqlCommand("insert into mel_line(RULENAME, MFG, DESCRIPT, DEVICE, CODE_2D, MODEL, EVALUATION, GOOD_PATH, GOOD_CLEANSE, NON_DEPLOY_PATH, NON_DEPLOY_CLEANSE, CRITICAL_NOTES, GOOD_COSMETICS, GOOD_BOM, GOOD_SPEC, GOOD_THRESHOLD, AUTHORIZED_BY, ACCT_CODE, LINE_STATUS) values('" + RULENAME.Text + "','" + MFG.Text + "','" + DESCRIPT.Text + "','" + DEVICE.Text + "','" + CODE_2D.Text + "','" + MODEL.Text + "','" + EVALUATION.Text + "','" + GOOD_PATH.Text + "','" + GOOD_CLEANSE.Text + "','" + NON_DEPLOY_PATH.Text + "','" + NON_DEPLOY_CLEANSE.Text + "','" + CRITICAL_NOTES.Text + "','" + GOOD_COSMETICS.Text + "','" + GOOD_BOM.Text + "','" + GOOD_SPEC.Text + "','" + GOOD_THRESHOLD.Text + "','" + AUTHORIZED_BY.Text + "','" + ACCT_CODE.Text + "','" + LINE_STATUS.Text + "',) ", con);
    cmd.ExecuteNonQuery();
    con.Close();
    Label1.Visible = true;
    Label1.Text = "New Row In MEL Added Successfully!";
    RULENAME.Text = "";
    MFG.Text = "";
    DESCRIPT.Text = "";
    DEVICE.Text = "";
    CODE_2D.Text = "";
    MODEL.Text = "";
    EVALUATION.Text = "";
    GOOD_PATH.Text = "";
    GOOD_CLEANSE.Text = "";
    NON_DEPLOY_PATH.Text = "";
    NON_DEPLOY_CLEANSE.Text = "";
    CRITICAL_NOTES.Text = "";
    GOOD_COSMETICS.Text = "";
    GOOD_BOM.Text = "";
    GOOD_SPEC.Text = "";
    GOOD_THRESHOLD.Text = "";
    AUTHORIZED_BY.Text = "";
    ACCT_CODE.Text = "";
    LINE_STATUS.Text = "";

}

答案 1 :(得分:0)

像这样使用..

select * from month as m
left join attend as a where m.date = a.date
left join attendout as o where m.date = o.date
where a.user_name = 'ali'
    and ... // conditions you want on..

答案 2 :(得分:0)

SELECT * FROM month

INNER JOIN attend

ON month.id = attend.id

INNER JOIN attendout

ON attend.id = attendout.id;