Quickest way to combine two identical structured tables without duplicate entries - mysql

I have two tables with the exact same structure/columns. I need to combine them without duplicates based on the second column (title). First column is ID and these may have duplicates but should be ignored.
Example of database structure
id (primary key, auto increment), title (unique), description, link
Ex. Table 1
1 Bob thisisbob bob.com
2 Tom thisistom tom.com
3 Chad thisischad chad.com
Ex. Table 2
1 Chris thisischris chris.com
2 Chad thisischad chad.com
3 Dough thisisdough doug.com
What I need in Table 3
1 Bob thisisbob bob.com
2 Tom thisistom tom.com
3 Chad thisischad chad.com
4 Chris thisischris chris.com
5 Dough thisisdough doug.com
Both tables have about 5 million entries/rows each. I need the most efficient way possible to combine them.

It is a little hard to understand exactly what you want. Perhaps, though, this might be:
select *
from table1
union all
select *
from table2
where not exists (select 1 from table1 where table1.title = table2.title);
This will run faster with an index on table1(title).
EDIT:
If you want to insert them into a third table, you can do:
create table table3 as
select *
from table1
union all
select *
from table2
where not exists (select 1 from table1 where table1.title = table2.title);

Related

MySQL Query with a status check from another table

There are three tables involved with this query.
Table1:
app_id
app_name
menu_id
1
BigApp
1
2
smallApp
2
3
theApp
2
Table2:
menu_id
menu_title
1
menu1
2
menu2
Table3:
user_id
app_id_list
1
1,2,3,4,5
2
1,3,5
So I want to grab the app_name, menu_title and then add another column (user_status, where would 1=on, 0=off) to verify that the app_id shows up in the app_id_list for a given user. The results for user_id = 2 would be:
app_name
menu_title
user_status
BigApp
menu1
1
smallApp
menu2
0
theApp
menu2
1
The SQL statement I've got so far is:
SELECT Table1.app_name, t2.menu_title
FROM Table1
INNER JOIN Table2 AS t2 ON (t2.menu_id = Table1.menu_id)
Not sure how to find the last column of data from Table3. Any thoughts?
After reviewing the following discussion:
Is storing a delimited list in a database column really that bad?
I have solved this issue by creating and expanding the Table3 where each value that was in the app_id_list now has its own row. Faster and can just link up with another JOIN.

Temp Table Creation in SQL

Could someone help me with this? I have couple of tables with some data. I need to query this table for the number of rows processed per day and load into another table:
Table1:
PNO ModelNo OrderNo CustID DAY
1 100012 1000AY 2345 31-AUG
2 109014 100YT8 3452 01-AUG
2 109014 100YT8 3452 31-AUG
Table2:
AN DAST CODE ROWS DAY
19 VEN EFD 19 31-AUG
21 EHT UYE 21 01-SEP
22 VEG WTE 24 01-SEP
Final Table:
DAY Source Rows
31-AUG Table1 2
01-SEP Table1 1
31-AUG Table2 1
01-SEP Table2 2
*Source: should be the table name.
Should I have to use Temp table or create a inner query concept and do it? Would like to know which is effecient. Please help.
Keep you updated: that all of these table are created under same schema..
As Shaharyar suggests, the reasons for actually creating such a table are probably questionable at best. However, the query to create the resulting table:
SELECT 'Table1' as Source, COUNT(*) as Rows, DAY FROM Table1 GROUP BY DAY
UNION
SELECT 'Table2' as Source, COUNT(*) as Rows, DAY FROM Table2 GROUP BY DAY
This doesn't scale very well for many tables. Though. Also, it would be preferable to add indexes on the DAY columns.
If you actually want to generate a manifested table, this might do the job:
CREATE TABLE final_table SELECT ...

Insert date from two tables in MySql

Consider the following two table
Table A:
id int auto_increment
with 2 rows of data.
id
1
2
Table B:
id auto_increment
aid reference to A.id
with 3 rows of data
id aid
1 1
2 2
3 2
If now the table A has been inserted 2 rows to
Table A:
id
1
2
3
4
So, how to write the insert statement so that it can insert to table B as result
Table B:
id aid
1 1
2 2
3 2
4 3
5 4
6 4
7 5
8 6
9 6
The question is for study only. I know there are many ways to do it, but I am just wondering if it can be done by using sql or just in mysql?
UPDATE
Sorry, the question since to be unclear. Let me state it in clear.
The data in table B has relation to table A. B.aid = A.id
The new data in table A, which is A.id, are in sequence, also has relation to the first two id. That means 1 and 3 with the same meaning, 2 and 4 also.
In the insertion of table B, it should consider both 1. and 2. That means, since with one aid=1 and two aid=2, then the data that needs to insert into table B is one aid=3, two aid=4, one aid=5 and two aid=6.
The question: This can be done easily with programming, however, I just wondering can 3. be done in a insert statement with out programming in Mysql?
Assuming you are inserting the ID(s) into Table A,
INSERT INTO [Table B](aid) VALUES ([THE ID])
Where this will insert the specified ID, in the aid column in Table B.
Otherwise if you want to match up the values you can use NOT IN to detect values that are not like, then you must insert these values,
This will select all IDs that do not exist in Table B:
SELECT id
FROM [Table A]
WHERE [Table A].id Not In (SELECT aid FROM [Table B])
...then later (depending what you're using it in, PHP? Then fletch the data (should look something like this))...
while($row = mysqli_fetch_assoc(...))
{
INSERT INTO [Table B](aid) VALUES($row['aid'])
}

Updating one table row with information from multiple rows in different table

I have searched quite a lot but haven´t found a solution to my problem. Now I hope someone can help me here.
I have two tables in an MySQL database and both tables have the columns Name and ArticleID
What I would like to do is to copy the content in the column Name from Table 1 to Table 2 where the ArticleID match. All the names should be separated by a comma in the column Name in Table 2.
In Table 1 there are for example 3 rows with the same content in the column Name but each row has a unique ArticleID. In 3 other rows there are a different Name but the ArticleID´s is the same as the first 3 rows.
Table 1
Name 1 - 1
Name 2 - 2
Name 3 - 3
Name 4 - 1
Name 5 - 2
Name 6 - 3
Table 2
1 - Name 1, Name 4
2 - Name 2, Name 5
3 - Name 3, Name 6
This would not normally be a problem for me, But now there are multiple rows with the same ArticleID and i can´t seem to figure it out by my self.
I hope you understand what I want :-)
Melker
INSERT INTO TABLE2(id, names)
SELECT ArticleID, GROUP_CONCAT(Name)
FROM TABLE1 GROUP BY ArticleID;
UPDATE
TABLE2
JOIN (SELECT ArticleID, GROUP_CONCAT(Name) AS Name FROM TABLE1 GROUP BY ArticleID) TABLE1
ON TABLE2.ArticleID = TABLE1.ArticleID
SET TABLE2.Name = TABLE1.Name
WHERE TABLE2.ArticleID = TABLE1.ArticleID
I actually had to use UPDATE and this worked

Retrieve unique data from MYSQL database

I have a table in my database which contains 5 rows. I am trying to write an sql statement that will retrieve all rows which only have 1 agency assigned to them.
case_id agency_ID
1 4
2 4
3 3
4 2
4 4
To clarify I would like to select the required rows (and any further rows) but only if the case_id is unique. Any rows with duplicates would be ommited.
I have tried to use DISTINCT(case_id), COUNT(*) to count all rows but it doesn't work and it's slowly sapping away my soul. It is probably an easy fix, but for the life of me I just can't see it.
Hope this is enough information to go on. Any help would be greatly appreciated.
SELECT * FROM your_table GROUP BY case_id HAVING COUNT(agency_ID) = 1
You can try
SELECT case_id,agency_ID,COUNT(case_id) as c
FROM yourTable
GROUP BY case_id
HAVING (c=1)