Php 用多个条件连接两个表
我有两个表,选择的月份为4月,会话为1 学生档案Php 用多个条件连接两个表,php,mysql,session,Php,Mysql,Session,我有两个表,选择的月份为4月,会话为1 学生档案 sud_id name session_id 1 kp 1 2 kishan 1 3 raj 1 4 arti 1 not any fee submitted till now 5 kp 2 session is diff
sud_id name session_id
1 kp 1
2 kishan 1
3 raj 1
4 arti 1 not any fee submitted till now
5 kp 2 session is diffrent
fee_id stud_id fee_balance delay name session_id feemonth
1 1 0 0 kp 1 april
2 2 10 10 kishan 1 april
3 2 0 0 kishan 1 april he cleared his balance
4 3 0 0 raj 1 march fee not submitted in this month
5 5 0 0 kp 1 april here session is diffrent... so nothing to do with this session
产生费用
sud_id name session_id
1 kp 1
2 kishan 1
3 raj 1
4 arti 1 not any fee submitted till now
5 kp 2 session is diffrent
fee_id stud_id fee_balance delay name session_id feemonth
1 1 0 0 kp 1 april
2 2 10 10 kishan 1 april
3 2 0 0 kishan 1 april he cleared his balance
4 3 0 0 raj 1 march fee not submitted in this month
5 5 0 0 kp 1 april here session is diffrent... so nothing to do with this session
我想向未提交费用或有任何余额或延迟的学生展示,直到所选月份(在本例中为4月),第1课时,这意味着我想展示如下:
arti and raj
我当前的查询:
select s.* from studentprofile s
left join
fee_generate f
on
s.stud_id=f.stud_id
where
s.session_id='$session_id' and s.stud_class='$class'
and
( f.stud_id is null or ( f.feemonth='$selected_fee_month1' and (f.delay>0.00 or f.fee_balance>0.00)))
order by s.name desc
我无法使用任何
join
编写查询。我的第二个问题是,两个表中的名称列都很常见。因此,所选名称列应来自studentprofile
表,并根据studentprofile
表按名称排序。尝试进行左外联接,在您的表中,您可以检查费用id是否为空(无记录)或延迟/费用余额是否大于0。
像这样
SELECT
s.*
FROM studentprofile s
LEFT OUTER JOIN fee_generate fg
ON fg.stud_id = s.sud_id
AND fg.feemonth='$selected_fee_month1'
WHERE
s.session_id='$session_id'
AND s.stud_class='$class'
AND (fg.fee_id IS NULL
OR fg.delay > 0
OR fg.fee_balance > 0)
未测试的代码
提示:您的feemonth字段不包含年份信息,我认为创建feeyear字段也是一个好主意。尝试在您的字段中进行左外连接,您可以检查费用id是否为空(无记录)或延迟/费用余额是否大于0。 像这样
SELECT
s.*
FROM studentprofile s
LEFT OUTER JOIN fee_generate fg
ON fg.stud_id = s.sud_id
AND fg.feemonth='$selected_fee_month1'
WHERE
s.session_id='$session_id'
AND s.stud_class='$class'
AND (fg.fee_id IS NULL
OR fg.delay > 0
OR fg.fee_balance > 0)
未测试的代码
提示:您的feemonth字段不包含年份信息,我认为创建feeyear字段也是一个好主意。嗨,Krishanpal,欢迎使用Stackoverflow!问题需要有一个明确的答案。如果您提供一个代码示例,可能有人可以帮助您完成这项工作。也值得一读。studentprofile表没有列stud_类,但在查询中使用它。你应该改正它,这样你的问题就更容易处理了。嗨,克丽珊帕尔,欢迎来到Stackoverflow!问题需要有一个明确的答案。如果您提供一个代码示例,可能有人可以帮助您完成这项工作。也值得一读。studentprofile表没有列stud_类,但在查询中使用它。你应该改正它,这样你的问题就更容易搞乱了。