简体   繁体   English

交叉检查两个表

[英]Cross check two tables

I have two table in my database, one contain attendance of employees while the other contains payroll of employees. 我的数据库中有两个表,一个表包含员工的出勤情况,另一个表包含员工的工资单。 inorder for an employee to get paid his name should exist on both table, my question is how to write the program that looks in the two table using id of an employee and list names that found ......am working with php and mysql 为了使一个雇员的名字可以被收入,这两个表都存在,我的问题是如何编写一个使用雇员的id和列出的名字在两个表中查找的程序……发现使用php和MySQL的

attendance table 考勤表

id   name        
1    mark

2    jhon

payroll table 工资表

id     name      salary
1      mark      20000$
2      jhon      3999$

You can use join query to fetch data from those tables. 您可以使用联接查询从那些表中获取数据。 If you do not know how to use it then take a look at the following link SQL Join tutorial 如果您不知道如何使用它,请查看以下链接SQL Join教程

Maybe you can use an JOIN. 也许您可以使用JOIN。 In this case a INNER JOIN should do the trick. 在这种情况下,INNER JOIN应该可以解决问题。 But there are also other kind of joins. 但是,还有其他类型的联接。

内部联接

Select all records from Table A and Table B, where the join condition is met. 从满足连接条件的表A和表B中选择所有记录。

左联接

Select all records from Table A, along with records from Table B for which the join condition is met (if at all). 从表A中选择所有记录,并从表B中选择满足连接条件的记录(如果有的话)。

正确加入

Select all records from Table B, along with records from Table A for which the join condition is met (if at all). 从表B中选择所有记录,并从表A中选择满足连接条件的记录(如果有的话)。

完全加入

Select all records from Table A and Table B, regardless of whether the join condition is met or not. 无论是否满足连接条件,请从表A和表B中选择所有记录。

In this case you can use the INNER JOIN like this 在这种情况下,您可以像这样使用INNER JOIN

SELECT attendance.name, payroll.salary FROM attendance INNER JOIN payroll ON attendance.name = payroll.name

This will select the name and the salary if they exist. 这将选择名称和薪水(如果存在)。

Maybe you should change the key names to the ones in your database 也许您应该将键名更改为数据库中的键名

For the record, got the images from http://www.sql-join.com/sql-join-types 为了记录,从http://www.sql-join.com/sql-join-types获取了图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM