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
Related
I want to combine three tables. Of which two multiple columns have to be joined together.
T1
id
name
1
Carl
2
Max
3
Bob
t2
id
t1_id
lastname
function
5
1
Johnsen
Welder
6
2
Clinten
Carpenter
7
3
Brink
Mason
t3
id
t2-id
Function
9
5
Metalworking
10
6
Cabinet maker
11
7
jointer
result
id
name
lastname
Function
1
Carl
Johnsen
Welder
1
Carl
Johnsen
Metalworking
2
Max
Clinten
Carpenter
2
Max
Clinten
Cabinet maker
3
Bob
Brink
Mason
3
Bob
Brink
jointer
my code is
Select t1.id, t1.naam, t2.lastname , t3.Function
From t1
left join t2 on t2.t1-id=t1.[id]
left join t3 on t3.t2-id=t2.[id],
(Select
Function
from t2
union
select
Function
from t3)t
You need a join with subquery for union based on id
Select
t1.id, t1.naam, t2.lastname, t.Function
From
t1
Left Join
t2 On t2.t1 - id = t1.[id]
Left Join
t3 On t3.t2 - id = t2.[id]
Inner Join
(Select id, Function
From t2
Union
Select id, Function
From t3) t On t.id = t1.id
I am thinking of using union all on two separate queries, one on two tables and one on three tables:
select t1.id, t1.name, t2.lastname, t2.function
from t1 join
t2
on t1.id = t2.t1_id
union all
select t1.id, t1.name, t2.lastname, t3.function
from t1 join
t2
on t1.id = t2.t1_id join
t3
on t2.id = t3.t2_id;
Note that no outer joins are needed. I am guessing that this would be better performing than an option with union/union all.
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
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.
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
I have one main table (t1)
id value group
------------------------------
5 22 1
6 55 1
7 18 2
8 11 2
And a cache table (t2)
id value group
------------------------------
1 12 1
2 30 1
3 18 2
4 11 2
The main table auto-increments, so everytime data is saved, the table is cleared and new ids are created, going up each time.
I need to update t2.id with t1.id so they are matching.
Required result for cache table(t2):
id value group
------------------------------
5 12 1
6 30 1
7 18 2
8 11 2
Attempt1:
UPDATE t1, t2 SET t1.id=t2.id WHERE t1.id < t2.id ORDER BY id ASC
Attempt2:
UPDATE t1, t2 SET t1.id = t2.id WHERE t1.id IS < MIN(t2.id) ORDER BY t1.id ASC
Attempt3:
UPDATE t1
INNER JOIN (
SELECT
MIN(t1.id) AS ID
FROM t1
GROUP BY ID) m ON t1.ID = m.ID
INNER JOIN t2 ON t1.ID = t2.ID
My attempt, try and let me know:
Update cache t2
set t2.id = t2.id +
(select min(t1.id)-min(t2.id) from main t1, cache t2);
EDIT:
If you could do two step query, its much easier.
DECLARE diffValue INTEGER;
SELECT min(t1.id)-min(t2.id) into diffValue from main t1, cache t2;
UPDATE cache t2 set t2.id = t2.id + diffValue;
You can't use joins in update syntax.
Firstly delete all records in the table:
DELETE * FROM t2;
Then write the new data in it:
INSERT INTO t2
(t2.id, t2.value, t2.group)
SELECT t1.id, t1.value, t1.group FROM t1
Is this what u wanted?