I have two tables t1 & t2.
table t1
user_id tags
1 a,b,c
2 b,c
table t2
user_id tags
1 d,c
2 c,d
I want to merge this into table t1. How can i do this.
table t1
user_id tags
1 a,b,c,d
2 b,c,d
I am new in sql.
Try this, although having more than one value in one field is not considered good practice and I couldn't recommend it.
SELECT t1.user_id, CONCAT(t1.tags, ', ', t2.tags) AS Tags
FROM table1 AS t1 INNER JOIN table2 AS t2 on t1.user_id = t2.user_id
This won't show unique values but will come close to what you request, which is hampered by your data format.
**
SQL Fiddle
**
Related
I have 3 tables. i'm looking to connect each table but unsure how.
I'm looking to get all the information from table 1 and from the other tables only if it matches the fields in table 1
table 1 (id, name_1, name_2, name_3, info_1, info_2
table 2 (description,info_1,otherstuff_1)
table 3 (name_1,name_2,name_3,random_1)
I have tried doing a join but feel that I was using the wrong syntax.
if done correctly. I'm hoping to get
id,name_1.table3,info_1.table2,name_2.table3
any help is appreciated
Table names appear before column names.
Try something like:
select t1.id, t3.name_1, t2.info_1, t3.name_2
from table1 t1, table2 t2, table3 t3
where t1.info_1 = t2.info_1
and t1.name_1 = t3.name_1
You can join tables by this query
select t1.id,t3.name1,t2.info_1,t3.name_2 from table1 as t1 join table2 as t2 on t1.info_1 = t2.info_1 join table3 as t3 on t1.name_1 = t3.name_1 and
t1.name_2 = t3.name_2 and t1.name_3.t3.name_3
I have two tables.
table 1
1
2
3
4
table 2
1
2
3
i want to select data from tables
table 1 have all data.
but table 2 may have all data or not.
so
if table 1 data exits in table two data select table 2 data otherwise select table one data.
if table 1 data not exit table 2 select table 1 data.
how can do that?
You need OUTER JOIN:
SELECT t1.ID,
COALESCE(t2.col1, t1.col1) AS col1, -- prefer data from table_2 if exists
COALESCE(t2.col2, t1.col2) AS col2,
-- ...
FROM table_1 t1 -- "table 1 have all data"
LEFT JOIN table_2 t2 -- "table 2 may have all data or not"
ON t1.ID = t2.ID;
Try FULL OUTER JOIN
SELECT t2.col1, t1.col1
FROM
table_1 t1 FULL OUTER JOIN table_2 t2
ON t2.col1 = t1.col1
Or, if full joins are not supported by your database, try emulating them.
What I am trying to do is join two tables, lets call them t1 and t2 on two columns. id and name, for this example. t1 will always have id and name, but t2 won't always have id and name. t1 has more columns like viewes, reports, and t2 has other columns that need to be joined. My question is, how can I show 0's for t2's columns if they don't exist?
I hav something similar to this, that joins tables only if both tables' rows have some value.
SELECT
date(t1.start_time) date,
t1.name,
t1.viewes,
t1.reports,
t2.col5,
t2.col6
from
table1 t1
left outer join table2 t2
on t2.name = t1.name and date(t2.start_time) = date(t1.start_time)
group by
1,2
order by
1 desc,
2 asc
;
I have lot's of experience with MySQL, but sometimes find that things need to be hacked to work correctly. What's your suggestion for this problem?
I don't know why I am confused with this query.
I have two table: Table A with 900 records and Table B with 800 records. Both table need to contain the same data but there is some mismatch.
I need to write a mysql query to insert missing 100 records from Table A to Table B.
In the end, both Table A and Table B should be identical.
I do not want to truncate all the entries first and then do a insert from another table. So please any help is appreciated.
Thank you.
It is also possible to use LEFT OUTER JOIN for that. This will avoid subquery overhead (when system might execute subquery one time for each record of outer query) like in John Woo's answer, and will avoid doing unnecessary work overwriting already existing 800 records like in user2340435's one:
INSERT INTO b
SELECT a.* FROM a
LEFT OUTER JOIN b ON b.id = a.id
WHERE b.id IS NULL;
This will first select all rows from A and B tables including all columns from both tables, but for rows which exist in A and don't exist in B all columns for B table will be NULL.
Then it filter only such latter rows (WHERE b.id IS NULL),
and at last it inserts all these rows into B table.
I think you can use IN for this. (this is a simpliplification of your query)
INSERT INTO table2 (id, name)
SELECT id, name
FROM table1
WHERE (id,name) NOT IN
(SELECT id, name
FROM table2);
SQLFiddle Demo
AS you can see on the demonstration, table2 has only 1 records but after executing the query, 2 records were inserted on table2.
If it's mysql and the tables are identical, then this should work:
REPLACE INTO table1 SELECT * FROM table2;
This will insert the missing records into Table1
INSERT INTO Table2
(Col1, Col2....)
(
SELECT Col1, Col2,... FROM Table1
EXCEPT
SELECT Col1, Col2,... FROM Table2
)
You can then run an update query to match the records that differ.
UPDATE Table2
SET
Col1= T1.Col1,
Col2= T1.Col2,
FROM
Table T1
INNER JOIN
Table2 T2
ON
T1.Col1 = T2.Col1
Code also works when a group by and having clauses are used. Tested SQL 2012 (11.0.5058) Tab1 is source with new records, Tab 2 is the destination to be updated. Tab 2 also has an Identity column. (Yes folks, real world is not as neat and clean as the lab assignments)
INSERT INTO Tab2
SELECT a.T1,a.T2,a.T3,a.T4,a.Val1,a.Val2,a.Val3,a.Val4,-9,-9,-9,-9,MIN(hits) MinHit,MAX(hits) MaxHit,SUM(count) SumCnt, count(distinct(week)) WkCnt
FROM Tab1 a
LEFT OUTER JOIN Tab2 b ON b.t1 = a.t1 and b.t2 = a.t2 and b.t3 = a.t3 and b.t4 = a.t4 and b.val1 = a.val1 and b.val2 = a.val2 and b.val3 = a.val3 and b.val4 = a.val4
WHERE b.t1 IS NULL or b.Val1 is NULL
group by a.T1,a.T2,a.T3,a.T4,a.Val1,a.Val2,a.Val3,a.Val4 having MAX(returns)<4 and COUNT(distinct(week))>2 ;
I'm reading an article about join in MySQL, but I don't understand why Joining tables through a series of relations has the advantage of reducing duplicate table reads.
Can someone explain with examples? Thanks.
Quoted text below:
Stated visually, these two ways of joining tables looks like:
Series of Relations: table 1 -> 2 -> 3
Common Relation: table 1 -> 2, (1) -> 3
Either way, MySQL reads table 1, then 2, then 3—its "single-sweep
multi-join method." The "(1)" in the common relation join means that
when table 3 is read, rows in it are found using its relation to table
1, not table 2 as in the series of relations join. Joining tables
through a series of relations has the advantage of reducing duplicate
table reads if the first table is the most restrictive, the second
table less restrictive, the third table even less restrictive, etc.
Text from hackmysql.com/case5
I believe it is simply referencing to the following two (unrealistic) examples, although I could be wrong.
Note the way in which table 3 is joined in each.
Table 1 -> 2 -> 3
SELECT t1.name, t2.phoneNo, t3.address
FROM table1 t1
JOIN table2 t2
ON t2.id = t1.id
JOIN table3 t3
ON t3.id = t2.id
WHERE t1.id = 123
Table 1 -> 2, (1) -> 3
SELECT t1.name, t2.phoneNo, t3.address
FROM table1 t1
JOIN table2 t2
ON t2.id = t1.id
JOIN table3 t3
ON t3.id = t1.id
WHERE t1.id = 123
Regards,
Chris