如何使用id从其他表中获取数据

时间:2015-08-24 11:36:33

标签: php mysql

如何从其他表中获取数据? 我有表'log'我想选择stud_id谁的'status_log'值为'in'和'out'而''status'的值为'0',我的问题我想选择有价值的学生'1表'学生''''年'。'

这是我现有的代码。

 delete []matrix;

这是数据库表。 日志

SELECT l1.stud_id FROM log AS l1
  JOIN log AS l2 ON l1.stud_id=l2.stud_id AND l2.status_log = 'out' AND l2.status = 0
  WHERE l1.status_log = 'in' AND l1.status = 0

学生

| stud_id | date_log  | time_log | ampm |status_log |status |
+---------+-----------+----------+------+-----------+-------+
| 123     |2015-08-19 | 07:38:34 | am   | in        | 0     |
| 123     |2015-08-19 | 07:40:34 | am   | out       | 0     |
| 5656    |2015-08-19 | 07:47:34 | am   | out       | 0     |
| 5656    |2015-08-19 | 07:47:34 | am   | out       | 1     |

thankyousomuch

3 个答案:

答案 0 :(得分:1)

You can try this :

SELECT st.cardcode as st FROM student INNER JOIN table as tb on tb.stud_id = st.cardcode WHERE
st.year = 1 AND tb.status_log IN ('in','out') AND tb.status = 0

WHERE clause specifies the following conditions : year is 1, status_log include in and out and status is 0

答案 1 :(得分:1)

这会在stud_id的{​​{1}}和in以及out中选择status_log status0year在学生表中带有1值。

SELECT a.stud_id, a.date_log, a.time_log, a.ampm, a.status_log, a.status, b.name, b.year, b.section, b.penalty_count
FROM log a
INNER JOIN student b ON a.stud_id = b.cardcode
WHERE (a.status_log = 'in' OR a.status_log = 'out') AND a.status = 0 AND b.year = 1

答案 2 :(得分:0)

SELECT l.stud_id
FROM log l
JOIN student s ON l.stud_id=s.cardcode 
WHERE l.status_log IN ('in','out') AND l.status=0 AND s.year=1
GROUP BY l.stud_id;