Deleting rows in a table with no primary key constraint - mysql

There are two tables A and B with same structure (number of columns, column names etc.). There is no primary key constraint for both A and B. Some of the columns values can be null but not mentioned as a constraint.
Creation of table looks like below
CREATE TABLE IF NOT EXISTS TableA
( col1 INT,
col2 VARCHAR(50)
col3 BIGINT )
I need to delete rows in A which are in B i.e A = A - B
There are around 100 columns in the original table (I have simplified it above). So listing all the columns is not desirable.
How do I do this task?
I had to add rows from another table C which I did by using INSERT INTO.
INSERT INTO tableA VALUES
(
SELECT * From tableC
EXCEPT
SELECT * from tableA
)

Use Left Join Of Table A with Table B and select all columns in
"ON" condition while joining then Select those rows or records which have null values in Table B's any column.
For Example:
TABLE A:
id name data
1 DAN 123
2 ANTONY 234
3 DAN 456
4 DAN 856
5 JOSEPH 546
6 ANTONY 784
7 JOSEPH 896
TABLE B:
id name data
1 DAN 123
5 JOSEPH 546
7 JOSEPH 896
QUERY for the above problem is:
SELECT A.id,A.name,A.data
FROM A
LEFT JOIN B
ON A.id = B.id
AND A.name = B.name
AND A.data = b.data
WHERE B.id IS NULL;
Result is:
id name data
2 ANTONY 234
3 DAN 456
4 DAN 856
6 ANTONY 784
You can also check this on below link:
http://sqlfiddle.com/#!9/d9e34b/4

delete from A where (column1, column2) not in
((
select column1, column2from A
minus
select column1, column2 from B
))
include all the columns from the table in select statement. As you have mentioned both table has same number of columns and names this query will work for you

You can delete data from A using inner join between A and B table,
Delete A
From A
Inner Join B
On A.id = B.Id
And A.name = B.name
And A.Data = b.data;
Try following Demo
Demo

Related

Select two names from another table by using id from the first table

I have two tables. Lets call them table_a and table_b
table_a
Id Name
3 John
4 Mary
8 Anna
table_b
Id Name1_Id Name2_Id Payment
23 3 8 300
24 4 3 200
25 8 3 300
How can i select something like this
name(which id equals to Name1_Id) , name(which id equals to Name2_Id), payment
Sorry for my bad english hope you can understand me.
You need two joins and table aliases:
select a1.name, a2.name, b.payment
from table_b as b join
table_a as a1
on b.name1_id = a1.id join
table_a as a2
on b.name2_id = a2.id

how to SELECT and Concat() column values based on several conditions SQL query?

newbie here to SQL. So I have two tables, let's take for example the two tables below.
Table A
set_num s_id s_val
100 3 AA
100 5 BB
200 3 AA
200 9 CC
Table B
s_id s_val phrase seq
1 DD 'hi' 'first'
3 AA 'hello' 'first'
6 EE 'goodnight' 'first'
5 BB 'world' 'second'
9 CC 'there' 'second'
4 FF 'bye' 'first'
I want to join Table A with Table B on two columns, like a composite key (s_id, s_val), and I want to return
set_num from Table A and the concatenation of phrases in Table B (which we will call entire_phrase, concat(...) AS entire_phrase).
The concatenation should also follow an order in which the phrases are to be concatenated. This will be determined by seq column in Table B for each phrase. "First" will indicate this phrase needs to come first and "Second", well comes next. I will like to do this with a SELECT query but not sure if this is possible without it getting to complex. Can I do this in SELECT or does this call for another approach?
Expected Output:
set_num entire_phrase
100 'hello world'
200 'hello there'
And not
set_num entire_phrase
100 'world hello'
200 'there hello'
Any help/approach will be greatly appreciated!
You can do it like this:
select temp1.set_num, concat(phrase1,' ',phrase2) as entire_phrase
from (
(
select set_num, b.phrase as phrase1
from TableA as A
join TableB as B
on a.s_id = b.s_id
and a.s_val = b.s_val
and b.seq = 'first'
) as temp1
join
(
select set_num, b.phrase as phrase2
from TableA as A
join TableB as B
on a.s_id = b.s_id
and a.s_val = b.s_val
and b.seq = 'second'
) as temp2
on temp1.set_num = temp2.set_num
)
Running here: http://sqlfiddle.com/#!9/d63ac3/1

How to insert null value when doing left join in SQL

I have these 2 table (each element is in column):
TABLE A:
ID 1 2 3 4 5
TD 20 10 0 1 7
TABLE B:
ID 2 3
TD 40 30
I want to do a LEFT JOIN to obtain a FINAL_TABLE in which I have all the ID od TABLE A and the one of table B in common with A. I want also that in case there is ID in TABLE A and not in TABLE B (ex. ID 1,4,5), in the FINAL_TABLE I will show NULL at that ID.
The FINAL_TABLE should be (see all in column):
ID 1 2 3 4 5
TDA 20 10 0 1 7
TDB NULL 40 30 NULL NULL
How can I achieve that?
Not quite sure if this is what you ask for, but perhaps you just want to add one result to another?
select 'TDA', col1, col2, col3, col4, col5 from tableA
UNION ALL
select 'TDB', null, col1, col2, null, null from tableB
I think this should work. Could you please try this once.
INSERT INTO FINAL_TABLE
(
ID,
tdA,
tdB
)
SELECT
A.ID,
A.td,
B.td
FROM
TABLEA
LEFT OUTER JOIN TABLEB ON (A.ID=B.ID)
GROUP BY
A.ID,
A.td,
B.td
;

Is it possible to join two tables into one single table , both tables have the same column names ...?

Is it possible to join two tables in mysql into one single table , both tables have the same column names ...?
table a has
id name state
1 jose up
2 sam mp
3 jack tn
table b is
id name state
4 ken ker
5 sk wb
is it possible to join both like:
id name state
1 jose up
2 sam mp
3 jack tn
4 ken ker
5 sk wb
Use UNION ALL
SELECT
id,name,state
from
tbla
UNION ALL
SELECT
id,name,state
from
tblb
If you want the exact duplicates to be excluded from the output. Use UNION
SELECT
id,name,state
from
tbla
UNION
SELECT
id,name,state
from
tblb
Reference:
13.2.8.4 UNION Syntax
The requirement you are describing is a union all query, not a join:
SELECT id, name, state
FROM table_a
UNION ALL
SELECT id, name, state
FROM table_b

left join not working - get records not in other table

I feel weird, as every other answer says it works, but I can't get the correct results :(
table A: id_num, name<br>
table B: id_num
table A has index on name, but not unique. id_num is unique in this table.<br>
table B has index on id_num, but not unique.
I want to get table A names, who are NOT in table B.
This not working:
**SELECT a.name FROM a
LEFT JOIN b ON (a.id_num = b.id_num)
WHERE b.id_numb IS NULL**
its returning names that ARE in table b (and some that are not).
this didn't work either:
**SELECT distinct(a.name)
FROM a where a.id_num
not in (select distinct(b.id_num) from b )**
I can't comprehend how a.names are being return who ARE IN table B, when the SQL says NOT IN.
what am I missing?
thanks
By right, the left join should work. And as well as in.
Here is a sample. So you may want to publish and show your table schema and data for more justice on your question. Best if you could just create the tables on SQLFIDDLE and show the reference. ;)
Here is just SAMPLE:-
SQLFIDDLE reference
Sample Tables:
tab1:
COL1 COL2
1 john
2 tim
3 jack
4 rose
tab2:
COL3 COL4
1 2
2 3
3 5
4 5
5 2
Query:
select * from tab1
where col1 not in
(select distinct col4 from tab2)
;
Results:
COL1 COL2
1 john
4 rose
As per OP's updated comments and table structure
OP mentioned the table tab1 will have multiple records for same name. According to his original table design, there's NO NAME field in the table tab2. It's also much recommended if OP had provided the expected results initially.
* SQLFIDDLE Reference
OP's table data:
COL1 COL2
1 john
2 tim
3 jack
4 rose
5 john
6 john
COL3 COL4
1 2
2 3
3 5
4 5
5 2
6 6
Query: When you do not want any name that is duplicated
select t.col1, t.col2
from tab1 t
join
(select t1.col2, count(*) as counts
from tab1 t1
group by t1.col2
having count(*) = 1) x
on t.col2 = x.col2
where t.col1 not in
(select distinct col4 from tab2)
;
Results: Here is Rose, the only record that has no duplicates and do not exist in tab2
COL1 COL2
4 rose
try this
select t1.name from table1 t1
where t1.name not in
(select t2.name from table2 t2 )
look the DEMO SQLFIDDLE
EDIT:
if you have only id_num in your second table then here look to
THIS DEMO SQLFIDDLE