How can I make one smaller table that overrides another bigger table? - mysql

I have to two table
table 1 table 2
id a b c id a
1 2 3 a 1 r
2 4 5 b 4 d
3 6 7 c 5 s
4 8 9 d
5 1 2 e
6 2 3 f
I want the table2 override table 1.
below is the result I want
I want to create a view table
table override
id a b c
1 r 3 a
2 4 5 b
3 6 7 c
4 d 9 d
5 s 2 e
6 2 3 f
How am I able to do that?
Thx

UPDATE table1
INNER JOIN table2 ON table1.id=table2.id
SET table1.a=table2.a
if You want view try thiz one :
select t1.id,ifnull(t2.a,t1.a),t1.b from table_1 as t1 left join table_2 as t2 on t2.id=t1.id
Creating View :
CREATE VIEW tbl_vw AS SELECT t1.id,COALESCE(t2.a,t1.a),b,c FROM table_1 as t1 left
JOIN table_2 as t2 ON t1.id = t2.id ;
Here is a Link for sample

Try this
UPDATE table1 t1 JOIN table2 ON t1.id = t2.id SET t1.a = t2.a
Change for View:
CREATE VIEW v AS SELECT t1.id,COALESCE(t2.a,t1.a),b,c FROM t1
LEFT JOIN t2 ON t1.id = t2.id ;
SQL FIDDLE

Use UPDATE with JOIN as shown below
UPDATE table1
INNER JOIN table2 ON table1.id=table2.id
SET table1.a=table2.a

Hope this help. Sorry because I did not have a chance to run in
select case when t.d is null then t.a else t.d end, t.b, t.c
from
(
select t1.a a, t1.b b , t1.c c, t2.a d
from table1 t1 left join table2 t2 on t1.id=t2.id
) t

Related

Sql query data filter between two table

Table1
ID Name
L2 abc
L3 xyz
L4 pqr
L5 ghi
Table 2
ID code
L4 1
L3 2
L4 4
L2 3
L4 1
In the above table, I want to output where I received all Id's with name and code, except for the id L4, which should not contain code 4.
You can use join and where condition
select a.id,a.name, b.code
from table1 a left join table2 b on a.id=b.id
and exists (select 1 from table2 c where a.id=c.id and c.code<>4 )
Try this!
select t1.id, t1.name, t2.code
from table1 t1
inner join table2 t2 on t1.id = t2.id
where (t2.id != 'L4' or t2.code != 4)

Select statement joining on multiple columns

I have a table with 3 columns that each contain an ID to another table, I am trying to find a way to pull back the name for each of those ID's but am unsure how I can do this using just sql, and not having to run in a php loop.
N1, N2, and N3 being the ID's of T2.
T1 T2
------------------ -----------------
ID N1 N2 N3 ID Name
1 2 3 1 1 Steve
2 3 2 1 2 Bob
3 1 2 3 3 Dan
My desired output would be
T1.id T1.N1 T1.N2 T1.N3
1 Bob Dan Steve
I'm not really sure where to begin on pulling the data this way. Any help is appreciated.
You can use LEFT JOIN for multiple times
SELECT t.ID,t21.Name,t22.Name,t23.Name
FROM table1 t
LEFT JOIN table2 t21 ON t21.ID=t.N1
LEFT JOIN table2 t22 ON t22.ID=t.N2
LEFT JOIN table2 t23 ON t23.ID=t.N3
Only three columns?
Simply try this...
select id,
(select T2.Name from T2 where T2.id = N1) as N1,
(select T2.Name from T2 where T2.id = N2) as N2,
(select T2.Name from T2 where T2.id = N3) as N3
from T1

using count and joins across multiple tables

Example:
Table 1:
id name
1 sim
2 sam
3 jas
Table 2
key age
1 10
1 20
2 40
3 10
Table 3:
id rating
2 7
2 6
3 8
3 7
1 9
Now, what I need is
number of rows that were grouped using group by in both the tables i.e table A and table B.
i.e
select t1.id, count(t2.key) as a, count(t3.id) as b
FROM
table1 t1
LEFT JOIN
table2 t2
ON
t1.id = t2.key
LEFT JOIN
table3 t3
ON
t1.id = t3.id
GROUP BY t1.key, t2.id
Result I expect:
id a b
1 2 1
2 1 2
3 1 2
You're getting duplicates because you're creating a cross product between all 3 tables. Use COUNT(DISTINCT) to filter out the duplicates.
select t1.id, count(DISTINCT t2.age) as a, count(DISTINCT t3.rating) as b
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.key
LEFT JOIN table3 t3 ON t1.id = t3.id
GROUP BY t1.id

mysql select from 3 tables where not all tables contain infos in the same amount of rows (Inner Join?!)

Looks like I am not able to understand some of the solutions given here and in other places, so I decided to ask my own question.
I have three tables. Like this:
table1
id name active
123 item1 1
234 item2 0
345 item3 1 <--- not in table2!!
456 item4 1
567 item5 1
table2
id item_id instock variants deliverytimes
1 123 0 S 21days
2 123 1 M 21days
3 123 2 L 21days
4 456 1 white 10days
5 456 0 black 10days
6 234 0 yellow sold
7 456 1 green sold
8 456 0 red sold
9 567 0 big sold
table3
id item_id description
1 123 Cool Shirt
2 234 Collectors Box
3 345 Comicbook
4 456 Basecap OneSize
5 567 Cool-Mug
I tried several attempts from LEFT JOIN to RIGHT JOIN etc. that all ended up with multiple results that are not DISTINCT in the ID of the first table that I need nor they were complete. Means that if table2 does not comntain item of table1 it won't show in the result.
The closest I got is this:
select * from table1 t1
INNER JOIN (
SELECT * FROM table2
where (deliverytimes!='sold' OR instock>0) LIMIT 1 ) t2
ON t1.id=t2.item_id,
table3 t3
where t1.active = '1'
and t1.id = t2.item_id and t3.item_id = t1.id;
In the end I need a list of:
"id, name, description"
result (as I would like it to be)
id name description
123 item1 Cool Shirt
345 item3 Comicbook
456 item4 Basecap OneSize
that does need to meet this requirements:
"item needs to be t1.active=1"
and
"if items has rows in t2 show only if one row equals (instock>0 or deliverytimes!=sold)"
and
"if item has no rows in t2 show as long as t1.active=1"
Last one is the problem. I never get distinct t1.id when I use other than inner join and with inner join I still miss the rows that are not present in t2 but are still active=1.
When starting to write a query think about what data you want to show. In your case you want to show data from table1 and table3, so join these and select from them. The criteria on table2 belong in the WHERE clause.
The criteria on table2 are:
either no entry in t2 exists
or an entry in t2 exists with instock > 0 or deliverytimes != sold
This means one EXISTS clause, one NOT EXISTS clause, both combined with OR.
select t1.id, t1.name, t3.description
from t1
join t3 on t3.item_id = t1.item_id
where t1.active = 1
and
(
not exists
(
select *
from t2
where t2.item_id = t1.item_id
)
or
exists
(
select *
from t2
where t2.item_id = t1.item_id
and (instock > 0 or deliverytimes != 'sold')
)
);
try this
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
if you want to filter you result with active = 1 and != soled filter in where clause
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
WHERE t1.active = 1 AND t2.deliverytimes != 'sold'
OR
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id AND t2.deliverytimes != 'sold'
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
WHERE t1.active = 1
You can try to link table1 with table2 using Left outer join (this will make sure you will get all records from table1). And now link tbale3 as inner join, this will make sure to get all records which are related with table1.
Table1 LEFT OUTER JOIN TABLE2 and Table1 INNER JOIN table3.
Above is my assumption, you can try the above logic.

How Do you add/substract from previous row in SQL?

In Mysql
Table
Id AA BBB
A 45 123
B 52 120
C 40 135
How would I get
B_A 7
A_C 5
First would need to sort by BBB Asc
then minus 52-45.
B_A concatenate B and A
How Do you add/substract form previous row in SQL?
If I needed to have seperate cols for B then A, How Would I add this.
SELECT concat(t1.id , '_', t2.id, ' ', t1.aa - t2.aa )
FROM table t1
INNER JOIN (SELECT Max(t2.id) prev_id,
t1.id
FROM table t1
INNER JOIN table t2
ON t1.id > t2.id
GROUP BY t1.id) prev
ON t1.id = prev.id
INNER JOIN table t2
ON t2.id = prev.prev_id