Home > database >  How to get A B in the table does not exist in A table of data is inserted into the table B, thank yo
How to get A B in the table does not exist in A table of data is inserted into the table B, thank yo

Time:10-06

First of all, thank you very much for reply,
Table A below
Id class
A, b
A, b1
C d
E f
=====
The following table B
Id class check
B, 1
A, b1 0
=====
Now, you can put A
in A tableC d
E f
Into the table b,
Thank you for your reply,

CodePudding user response:


 INSERT INTO BSELECT *, 
0
FROM A
WHERE NOT the EXISTS (SELECT *
The FROM B
WHERE Anderson, d=B.i d
AND biggest lass=A.c lass);

SELECT * FROM B

CodePudding user response:

Thank you,
I hope B table the results are as follows:
The following table B
Id class check
B, 1
A, b1 0
c
E f

CodePudding user response:

The following table B
Id class check
B, 1
A, b1 0
0
E 0 f



Check field, just from A table to insert, the initial value is 0, then under the condition of the other, will be updated into 1

CodePudding user response:

First of all, watch A, namely view_aaa data, the following
SELECT the uid, class2 FROM view_aaa; This sentence, perform normal
Uid class2
4 134
4 135
5 135
6 135
===============

Then, see table B, namely shizi_certificate_member authorized (teachers) of the first two columns of data, from the table A uid, class2, initially empty.

The following statement refers to the normal line:
The SELECT view_aaa uid, view_aaa. Class2 FROM view_aaa, shizi_certificate_member
WHERE view_aaa. Uid=shizi_certificate_member. Uid
And view_aaa. Class2=shizi_certificate_member. Columnid
==============================
Uid class2
The result is empty, because shizi_certificate_member itself is empty,

Finally, the above two SQL statements combined:
SELECT uid, class2 FROM view_aaa
WHERE NOT EXISTS (the SELECT view_aaa uid, view_aaa. Class2 FROM view_aaa, shizi_certificate_member
WHERE view_aaa. Uid=shizi_certificate_member. Uid
And view_aaa. Class2=shizi_certificate_member. Columnid);


The results are as follows:
[SQL] # SELECT * FROM view_aaa
# the SELECT uid, class2, 0 FROM view_aaa;

SELECT the uid, class2 FROM view_aaa
WHERE NOT the EXISTS (SELECT view_aaa uid, view_aaa. Class2 FROM view_aaa, shizi_certificate_member
WHERE view_aaa. Uid=shizi_certificate_member. Uid
And view_aaa. Class2=shizi_certificate_member. Columnid);
(Err), 1064 - You have an error in your SQL syntax; Check the manual that corresponds to your MySQL server version for the right syntax to use near '(the SELECT view_aaa. Uid, view_aaa class2 FROM view_aaa, shizi_certificate_member
'at line 5

I haven't understand, why wrong,
Also please come to help me, thank you very much.





CodePudding user response:

MYSQL version is what? Whether to support the NOT EXISTS

CodePudding user response:

Not the exists to perform, there is no support problem,

CodePudding user response:

I dealt with, the following script,

Insert into shizi_certificate_member (uid, columnid)
The SELECT view_aaa uid, class2 FROM view_aaa
WHERE NOT the EXISTS (
The select view_aaa uid, view_aaa. Class2 from shizi_certificate_member, view_aaa
WHERE view_aaa. Uid=shizi_certificate_member. Uid
And view_aaa. Class2=shizi_certificate_member. Columnid)

Critical advice please, see if there are any errors,
Thank you for your reply,

CodePudding user response:

 
Insert into shizi_certificate_member (uid, columnid)
The SELECT view_aaa uid, class2 FROM view_aaa
WHERE NOT the EXISTS (
Select 1 from shizi_certificate_member, view_aaa
WHERE view_aaa. Uid=shizi_certificate_member. Uid
And view_aaa. Class2=shizi_certificate_member. Columnid)
  • Related