I have two column with the same name in different tables.
I want to join them into one column in a view.
Here my first table stocks:
+----------+------------+------------+---------+----------------+--------+
| stock_id | stock_cost | stock_left | item_id | purchasedtl_id | trx_id |
+----------+------------+------------+---------+----------------+--------+
| 1 | 1000 | 0 | 1 | 1 | 1 |
| 2 | 1000 | 5 | 1 | 2 | 2 |
| 3 | 1000 | 1 | 1 | 3 | 4 |
+----------+------------+------------+---------+----------------+--------+
Second table stocks_out
+-------------+----------------+--------------+---------+----------+------------+--------+
| stockout_id | stockout_price | stockout_qty | item_id | stock_id | saledtl_id | trx_id |
+-------------+----------------+--------------+---------+----------+------------+--------+
| 1 | 2000 | 1 | 1 | 1 | 1 | 3 |
+-------------+----------------+--------------+---------+----------+------------+--------+
And I want to join them to be like this trx_id, trx_no, trx_closetime, trx_type stock_id, stock_cost, stockout_id, stock_out_cost, stockout_price, item_id
item_id is the field I want to join in one column.
The current Query is :
select `transactions`.`trx_id` AS `trx_id`,`transactions`.`trx_no` AS `trx_no`,`transactions`.`trx_closetime` AS `trx_closetime`,`transactions`.`trx_type` AS `trx_type`,`stocks`.`stock_id` AS `stock_id`,`stocks`.`stock_cost` AS `stock_cost`,`stock_out`.`stockout_id` AS `stockout_id`,`stock_out`.`stockout_price` AS `stockout_price` from ((`transactions` left join `stocks` on(`stocks`.`trx_id` = `transactions`.`trx_id`)) left join `stock_out` on(`stock_out`.`trx_id` = `transactions`.`trx_id`)) order by `transactions`.`trx_closetime`;
And the current result:
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
| trx_id | trx_no | trx_closetime | trx_type | stock_id | stock_cost | stockout_id | stockout_price |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
| 1 | 02002-02-170415-001 | 2017-04-15 19:40:03 | 2 | 1 | 1000 | NULL | NULL |
| 2 | 02002-02-170415-002 | 2017-04-15 19:40:13 | 2 | 2 | 1000 | NULL | NULL |
| 3 | 02002-01-170415-001 | 2017-04-15 19:40:57 | 1 | NULL | NULL | 1 | 2000 |
| 4 | 02002-02-170415-003 | 2017-04-15 19:41:14 | 2 | 3 | 1000 | NULL | NULL |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
Found it guys.
I just need to add the following query as the column
COALESCE(`stocks`.`item_id`, `stocks_out`.`item_id`) AS `item_id`
So the query will be like
select `transactions`.`trx_id` AS `trx_id`,`transactions`.`trx_no` AS `trx_no`,`transactions`.`trx_closetime` AS `trx_closetime`,`transactions`.`trx_type` AS `trx_type`,`stocks`.`stock_id` AS `stock_id`,`stocks`.`stock_cost` AS `stock_cost`,`stock_out`.`stockout_id` AS `stockout_id`,`stock_out`.`stockout_price` AS `stockout_price`, COALESCE(`stocks`.`item_id`, `stocks_out`.`item_id`) AS `item_id from ((`transactions` left join `stocks` on(`stocks`.`trx_id` = `transactions`.`trx_id`)) left join `stock_out` on(`stock_out`.`trx_id` = `transactions`.`trx_id`)) order by `transactions`.`trx_closetime`;
And the result:
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
| trx_id | trx_no | trx_closetime | trx_type | stock_id | stock_cost | stockout_id | stockout_price | item_id |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
| 1 | 02002-02-170415-001 | 2017-04-15 19:40:03 | 2 | 1 | 1000 | NULL | NULL | 1 |
| 2 | 02002-02-170415-002 | 2017-04-15 19:40:13 | 2 | 2 | 1000 | NULL | NULL | 1 |
| 3 | 02002-01-170415-001 | 2017-04-15 19:40:57 | 1 | NULL | NULL | 1 | 2000 | 1 |
| 4 | 02002-02-170415-003 | 2017-04-15 19:41:14 | 2 | 3 | 1000 | NULL | NULL | 1 |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
Related
How can I merge multiple rows with same ID into one row.
I have table:
+----+------+------+------+
| ID | A | B | C |
+----+------+------+------+
| 1 | 123 | 31 | 456 |
| 1 | 412 | NULL | 1 |
| 2 | 567 | 38 | 4 |
| 2 | 567 | NULL | NULL |
| 3 | 2 | NULL | NULL |
| 3 | 5 | NULL | NULL |
| 4 | 6 | 1 | NULL |
| 4 | 8 | NULL | 5 |
| 4 | NULL | NULL | 5 |
+----+------+------+------+
I want to have table :
I have table:
+----+-----+------+------+-----+------+------+------+------+----+
| ID | A | B | C | A2 | B2 | C2 | A3 | B3 | C3 |
+----+-----+------+------+-----+------+------+------+------+----+
| 1 | 123 | 31 | 456 | 412 | NULL | 1 | | | |
| 2 | 567 | 38 | 4 | 567 | NULL | NULL | | | |
| 3 | 2 | NULL | NULL | 5 | NULL | NULL | | | |
| 4 | 6 | 1 | NULL | 8 | NULL | 5 | NULL | NULL | 5 |
+----+-----+------+------+-----+------+------+------+------+----+
Try This:-
1 :- Please Change data table structure to disallow null value.
Query :-
SELECT
GROUP_CONCAT(if (`A` ='0', 'NOVAL', `A`)SEPARATOR '----') as A,
GROUP_CONCAT(if (`B` ='0', 'NOVAL', `B`)SEPARATOR '----') as B,
GROUP_CONCAT(if (`C` ='0', 'NOVAL', `C`)SEPARATOR '----') as C
FROM que1
GROUP BY id
Now You can get the value using mysql_fetch_array
I'm trying to use group by clause to update the dt column based on column 'last'. I need find last date for group 'group by hid,tid,tdate,fid,did,depid,acc' and set 'dt' value for all record in this group.
For example:
| id | hid | tid | tdate | fid | did | p2 | depid | acc | dt | last |
|----------|-------|-----|------------|-----|-----|-------|-------|------|------|----------------------|
| 3742030 | 12332 | 1 | 2017-09-02 | 1 | 1 | 9560 | 1 | 5334 | 1 | 2016-11-03T09:00:20Z |
| 3799297 | 2386 | 1 | 2017-08-29 | 1 | 1 | 8480 | 1 | 5352 | 1 | 2016-11-03T11:12:55Z |
| 4848877 | 2386 | 1 | 2017-08-29 | 1 | 1 | 8720 | 1 | 5352 | 2369 | 2016-12-17T16:59:22Z |
| 10706343 | 12332 | 1 | 2017-09-02 | 1 | 1 | 9660 | 1 | 5334 | 2065 | 2017-03-01T12:32:27Z |
| 14546682 | 2386 | 1 | 2017-08-29 | 1 | 1 | 11720 | 1 | 5352 | 4431 | 2017-05-12T10:24:09Z |
| 15824920 | 12332 | 1 | 2017-09-02 | 1 | 1 | 10820 | 1 | 5334 | 1111 | 2017-07-15T05:19:04Z |
to
| id | hid | tid | tdate | fid | did | p2 | depid | acc | dt | last |
|----------|-------|-----|------------|-----|-----|-------|-------|------|------|----------------------|
| 3742030 | 12332 | 1 | 2017-09-02 | 1 | 1 | 9560 | 1 | 5334 | 1111 | 2016-11-03T09:00:20Z |
| 3799297 | 2386 | 1 | 2017-08-29 | 1 | 1 | 8480 | 1 | 5352 | 4431 | 2016-11-03T11:12:55Z |
| 4848877 | 2386 | 1 | 2017-08-29 | 1 | 1 | 8720 | 1 | 5352 | 4431 | 2016-12-17T16:59:22Z |
| 10706343 | 12332 | 1 | 2017-09-02 | 1 | 1 | 9660 | 1 | 5334 | 1111 | 2017-03-01T12:32:27Z |
| 14546682 | 2386 | 1 | 2017-08-29 | 1 | 1 | 11720 | 1 | 5352 | 4431 | 2017-05-12T10:24:09Z |
| 15824920 | 12332 | 1 | 2017-09-02 | 1 | 1 | 10820 | 1 | 5334 | 1111 | 2017-07-15T05:19:04Z |
Schema:
http://sqlfiddle.com/#!9/4fad1d/1
Is there some way to update all rows in the table based on group?
Thanks
Join the table with the subquery that will find the most recent row per group.
update of t
join (
select t1.*
from of t1
join (
select hid,tid,tdate,fid,did,depid,acc, max(last) as last
from of
group by hid,tid,tdate,fid,did,depid,acc
) t2 using (hid,tid,tdate,fid,did,depid,acc,last)
) t3 using (hid,tid,tdate,fid,did,depid,acc)
set t.dt = t3.dt;
http://sqlfiddle.com/#!9/93708/2
For the join in the subquery you can also use NATURAL JOIN
natural join (
select hid,tid,tdate,fid,did,depid,acc, max(last) as last
from of
group by hid,tid,tdate,fid,did,depid,acc
) t2
http://sqlfiddle.com/#!9/e7e5ee/1
I have this table, its name is puntajes:
+---------------+---------------+---------+
| estudiante_ID | evaluacion_ID | puntaje |
+---------------+---------------+---------+
| 1 | 1 | 15 |
| 2 | 1 | 11 |
| 3 | 1 | 17 |
| 4 | 1 | 12 |
| 1 | 2 | 13 |
| 2 | 2 | 8 |
| 3 | 2 | 15 |
| 4 | 2 | 16 |
| 1 | 3 | 9 |
| 2 | 3 | 14 |
| 3 | 3 | 9 |
| 4 | 3 | 10 |
| 1 | 4 | 15 |
| 2 | 4 | 16 |
| 3 | 4 | 9 |
| 4 | 4 | 12 |
+---------------+---------------+---------+
And I want to get the max score from puntaje column where evaluacion_ID is equal to 3, i want this value to be in the 'maxpuntaje 'column in the evaluaciones table, like an update , this is evaluaciones table:
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| fecha | date | NO | | NULL | |
| tipo | enum('P','E') | NO | | NULL | |
| maxpuntaje | int(11) | NO | | NULL | |
| clase_ID | int(10) unsigned | NO | | NULL | |
| evaluacion_ID | int(10) unsigned | NO | PRI | NULL | auto_increment |
+---------------+------------------+------+-----+---------+----------------+
both of the tables have the evaluacion_ID column
I've commented out WHERE conditions that limit the UPDATE and maximum puntaje value retrieval so that your query could be run for all evaluacion_ID. If you need it only for 3, remove the comment marks.
Using MySQL UPDATE JOIN syntax:
UPDATE evaluaciones e
LEFT JOIN (
SELECT evaluacion_ID, MAX(puntaje) maxpuntaje
FROM puntajes
-- WHERE evaluacion_ID = 3
GROUP BY evaluacion_ID
) p USING (evaluacion_ID)
SET maxpuntaje = p.maxpuntaje
-- WHERE e.evaluacion_ID = 3
I suspect you're looking for this query:
UPDATE evaluaciones
SET maxpuntaje = (
SELECT max(puntaje)
FROM puntajes
WHERE evaluacion_ID = evaluaciones.evaluacion_ID
)
WHERE evaluacion_ID = 3
Say I have 3 tables, foo bar and baz, where bar and baz having different additional informations about the datasets in foo. Is there a way to join these 3 tables together, so that in every output row is either a (foo <join> bar) or (foo <join> baz) dataset?
Say, I have the following tables:
mysql> select * from foo;
+----+-------+
| id | foo |
+----+-------+
| 1 | start |
| 2 | mid |
| 3 | end |
+----+-------+
mysql> select * from bar;
+----+-----+-----------+
| id | bid | bar |
+----+-----+-----------+
| 1 | 1 | bar-start |
| 2 | 2 | bar-mid |
| 3 | 3 | bar-end |
+----+-----+-----------+
mysql> select * from baz;
+----+-----+-------------+
| id | bid | baz |
+----+-----+-------------+
| 1 | 1 | baz-start-1 |
| 1 | 2 | baz-start-2 |
| 1 | 3 | baz-start-3 |
| 2 | 4 | baz-mid-1 |
| 2 | 5 | baz-mid-2 |
| 2 | 6 | baz-mid-3 |
| 3 | 7 | baz-end-1 |
| 3 | 8 | baz-end-2 |
| 3 | 9 | baz-end-3 |
+----+-----+-------------+
I can extract all the information with a query which joins those tables together via
mysql> select * from foo join bar join baz on foo.id = bar.id and foo.id=baz.id;
+----+-------+----+-----+-----------+----+-----+-------------+
| id | foo | id | bid | bar | id | bid | baz |
+----+-------+----+-----+-----------+----+-----+-------------+
| 1 | start | 1 | 1 | bar-start | 1 | 1 | baz-start-1 |
| 1 | start | 1 | 1 | bar-start | 1 | 2 | baz-start-2 |
| 1 | start | 1 | 1 | bar-start | 1 | 3 | baz-start-3 |
| 2 | mid | 2 | 2 | bar-mid | 2 | 4 | baz-mid-1 |
| 2 | mid | 2 | 2 | bar-mid | 2 | 5 | baz-mid-2 |
| 2 | mid | 2 | 2 | bar-mid | 2 | 6 | baz-mid-3 |
| 3 | end | 3 | 3 | bar-end | 3 | 7 | baz-end-1 |
| 3 | end | 3 | 3 | bar-end | 3 | 8 | baz-end-2 |
| 3 | end | 3 | 3 | bar-end | 3 | 9 | baz-end-3 |
+----+-------+----+-----+-----------+----+-----+-------------+
But I would like to get an output like the following table, since that would make the code in the application consuming the data much simpler:
+----+-------+------+------+-----------+------+------+-------------+
| id | foo | id | bid | bar | id | bid | baz |
+----+-------+------+------+-----------+------+------+-------------+
| 1 | start | 1 | 1 | bar-start | NULL | NULL | NULL |
| 1 | start | NULL | NULL | | 1 | 1 | baz-start-1 |
| 1 | start | NULL | NULL | | 1 | 2 | baz-start-2 |
| 1 | start | NULL | NULL | | 1 | 3 | baz-start-3 |
| 2 | mid | 2 | 2 | bar-mid | NULL | NULL | NULL |
| 2 | mid | NULL | NULL | | 2 | 4 | baz-mid-1 |
| 2 | mid | NULL | NULL | | 2 | 5 | baz-mid-2 |
| 2 | mid | NULL | NULL | | 2 | 6 | baz-mid-3 |
| 3 | end | 3 | 3 | bar-end | NULL | NULL | NULL |
| 3 | end | NULL | NULL | | 3 | 7 | baz-end-1 |
| 3 | end | NULL | NULL | | 3 | 8 | baz-end-2 |
| 3 | end | NULL | NULL | | 3 | 9 | baz-end-3 |
+----+-------+------+------+-----------+------+------+-------------+
Is there a way to convince mysql to give me a result set like the last one?
(
select foo.id as foo_id, foo.foo, bar.id, bar.bid, bar.bar, null, null as baz_bid, null as baz from foo
join bar on foo.id=bar.id
)
union
(
select foo.id as foo_id, foo.foo, null, null, '', baz.id, baz.bid as baz_bid, baz.baz from foo
join baz on foo.id=baz.id
)
order by foo_id asc, bar desc, baz_bid asc
For example,I have 3 tables;
table1:
+-------+
| count |
+-------+
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
+-------+
table2:
+-------+
| count |
+-------+
| 3 |
| 0 |
| 0 |
| 0 |
| 0 |
+-------+
table3:
+-------+
| count |
+-------+
| 1 |
| 1 |
| 0 |
| 0 |
| 1 |
+-------+
I want to calculate table1.count+table2.count+table3.count, to get the result,table_right:
+-------+
| count |
+-------+
| 5 | (1+3+1=5)
| 1 | (0+0+1=1)
| 0 | (0+0+0=0)
| 0 | (0+0+0=0)
| 4 | (3+0+1=4)
+-------+
However, if I use command :
select table1.count+table2.count+table3.count as total
from table1,table2,table3;
The result will become to:
+-------+
| total |
+-------+
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 4 |
| 3 |
| 3 |
| 3 |
| 6 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 4 |
| 3 |
| 3 |
| 3 |
| 6 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
+-------+
This is not the result i want, If I try
select distinct table1.count+table2.count+table3.count as total
from table1,table2,table3;
I will get:
+-------+
| total |
+-------+
| 5 |
| 4 |
| 7 |
| 2 |
| 1 |
| 3 |
| 6 |
| 0 |
+-------+
Still isn't the result I want. How could i do to get table_right?
if you add a common id (lets call id rowId and lets assume it has the same name on every table),
SELECT t1.count + t2.count + t3.count AS total
FROM table1 AS t1
LEFT JOIN table2 AS t2 using (rowId)
LEFT JOIN table3 AS t3 using (rowId)
if you not have those ids, all i can think about its summing all t1 then all t2 then all t3 and finally add the results together.
SELECT t1+t2+t3 as total
FROM (SELECT (SELECT SUM(count) from table1) as t1,
(SELECT SUM(count) from table2) as t2,
(SELECT SUM(count) from table3) as t3
)
Check out this SQLFiddle
EDIT (2)
to add rowId just alter the tables:
ALTER TABLE table1 ADD COLUMN rowId int not null auto_increment primary key;
ALTER TABLE table2 ADD COLUMN rowId int not null auto_increment primary key;
ALTER TABLE table3 ADD COLUMN rowId int not null auto_increment primary key;