Home > database >  Writing about SQL statements
Writing about SQL statements

Time:09-27

Currently has a staff table has staff staffId,
There is a table inside with the employee's social security record of social security, had a staffId field associated with employee table, and table a deleteFlag field 0 of social security is not deleted, 1 is deleted, if the employee didn't sign the social security is not exist the staffId social security records,
Now I want to write a SQL, is found to staff to record all did not sign a contract, if employees have social security but deleted at the beginning, there is only a record, and deleteFlag of 1 is also needs to be found out,
The SQL what to write, the great god,

CodePudding user response:

Is not did not sign a contract, is not sign of social security

CodePudding user response:

 SELECT 
*
The FROM
Employee table
WHERE
NOT the EXISTS (SELECT 1 FROM social security table WHERE the staff table. StaffId=social security table. StaffId AND social security table. DeleteFlag=0)

CodePudding user response:

Found to all employees, employee ID is not yet buy social security staff ID in it:
Select * from employee table where staffid not in (select staffid from social security table where deleteFlag='0');

CodePudding user response:

Reference
suggest you list your table structure, and provide the test data, and based on these test data that corresponds to the correct result,
Refer to the way the post questions http://bbs.csdn.net/topics/320211382

1. You create table XXX.. Statement
2. You insert into XXX... Statement
3. The result is what kind, and give a simple algorithm description)
4. Do you use the database name and version of (often asked in MS SQL server edition MySQL)

So people who want to help you to build the same environment with you directly, and in providing the test before, avoid the error in the text understanding,
.
  • Related