update from select result - mysql

I tried to update the color in table tbl of colors in the item table based on field item_id, with the following query. but I get an error Sql error (1242).
update tbl mt
left join tbl_detail dt on mt.tbl_No = dt.tbl_No
set dt.color_Id =
(
select p.color_ID
from ( select dt.item_Id, dt.color_Id
from tbl mt
left join tbl_detail dt on mt.tbl_No = dt.tbl_No
where mt.Tipe = 2 ) h
left join itemp on h.item_id = p.item_id
)
where mt.Tipe = 2 ;
I want to update a field color in the table tbl with existing color in the item table. How I should make it?
I have tried a simple syntax like this but then I get sql error (1442)
update tbl mt
left join tbl_detail dt on mt.tbl_No = dt.tbl_No
left join item p on dt.item_Id = p.item_Id
set dt.color_Id = p.color_id
where mt.Tipe = 2 ;
Example :
table tbl:
|tbl_No|tipe|
1 2
2 2
3 2
table tbl_detail:
|tbl_detail_No|tbl_No|item_id|color_id|
1 1 1 null
2 1 2 null
1 2 3 null
2 2 4 null
table item:
|item_id|color_id|
1 1
2 2
3 3
4 4
I want to update color_id in table tbl_detail based on item_id in table item; after the update, the table tbl_detail should be as follows.
table tbl_detail:
|tbl_detail_No|tbl_No|item_id|color_id|
1 1 1 1
2 1 2 2
1 2 3 3
2 2 4 4
(oh, I'm really bad at explaining it, hope someone can understand as English is not my first language.)
Thanks for all the help and advice.

Related

Identify missing conversions with an SQL query

I need to find a way to find what conversions are missing.
I have three tables.
Table 1: type, which are the different types.
id
name
1
typeA
2
typeB
3
typeC
Table 2: section, which are the different sections.
id
name
1
section1
2
section2
3
section3
4
section4
Table 3: conversions, which contains all the combinations to go from one type to another for each of the sections.
id
section_id
type_convert_from
type_convert_to
1
1
1
2
2
2
1
2
3
3
1
2
4
4
1
2
5
1
1
3
6
2
1
3
7
3
1
3
8
4
1
3
9
1
2
1
10
2
2
1
11
3
2
1
12
4
2
1
For example some are missing from the table above, how can I identify them with a SQL query? Thanks!
Try this. The cross join of table type with itself generates all possible combinations of type id's. I've excluded combinations in the cross join where id_from = id_to (ie you're not interested in conversions from a type to itself)
select * from conversions C
right join (
select T1.id as id_from, T2.id as id_to
from type T1 cross join type T2
where T1.id <> T2.id
) X on X.id_from = C.type_convert_from and X.id_to = C.type_convert_to
where C.type_convert_from is null
If you want to check missing type conversions by section, extend the cross join by adding the section table to include section.id as follows. It will list missing type conversions within each section.
select X.section_id, X.id_from, X.id_to from conversions C
right join (
select S.id as section_id, T1.id as id_from, T2.id as id_to
from types T1 cross join types T2 cross join section S
where T1.id <> T2.id
) X
on X.id_from = C.type_convert_from and X.id_to = C.type_convert_to
and C.section_id = X.section_id
where C.type_convert_from is null

How to subtract data from table2 to table1

I'm trying to subtract and add existing values in table like this
Table 1
id q q_out
1 10 0
2 10 0
Table 2
id q
1 2
1 1
2 1
2 2
I am expecting this output when i update table 1:
id q q_out
1 7 3
2 7 3
but I get this output:
id q q_out
1 8 2
2 9 1
this is my query:
UPDATE
db_pro d
JOIN cart c ON d.pro_num = c.p_num
SET
d.q = (d.q - c.q),
d.out_q = (d.out_q + c.q)
WHERE
c.s_num='13-37478' and
c.class not like 'Books' and
c.remarks like 'On Process'
You need to join the agregated values of table cart:
UPDATE
db_pro d
JOIN (
select p_num, sum(q) q from cart
WHERE
s_num='13-37478' and
class not like 'Books' and
remarks like 'On Process'
group by p_num
) c ON d.pro_num = c.p_num
SET
d.q = (d.q - c.q),
d.out_q = (d.out_q + c.q)

Joining the table creating a duplicate entry in laravel

I just want to merge the content of 2 tables and display it based on the ID.
Both the table has 3 entries.
Table a - Sampling order
Date Docname Products Quantity ID
1 A A 1 1
2 B B 2 1
3 C C 3 1
Table B - Representative locations
Date Area lat long ID
1 a 1 1 1
2 b 2 2 1
3 c 3 3 1
The output should generate like 3 rows with all the table A columns and B columns where ID = Specified ID
I need a output like this
Date Docname product Quantity Area lat long
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
But its generating 9 rows (3*3) and duplicating the numbers of rows present in both the tables.
Its generating
Date Docname product Quantity Area lat long
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
1 A A 1 a 1 1
2 B B 2 b 2 2
3 C C 3 c 3 3
Combing number of rows in A * B - I just need only 3 rows with respect to ID.
Query -
$Report = DB::table('sampling_order')
->join('representativelocations','representativelocations.representativeid','=','sampling_order.representativeid')
->select('sampling_order.representativeid as representativeid',
'sampling_order.date as date',
'sampling_order.doctor_name as doctor_name',
'sampling_order.products as products',
'sampling_order.quantity as quantity',
'representativelocations.latitude as latitude',
'representativelocations.longitude as longitude',
'representativelocations.area as area')
->whereBetween('sampling_order.date', [$Datefrom, $Dateto])
->where('sampling_order.representativeid','=',$Representativeid)->get();
What I can see, all Id is '1' in two tables,
so it should be 3 * 3 = 9 rows in result.
I guess you may want this:
id product area-A area-B area-C
1____A____1____2_____3
1____B____2____3_____4
if so. you need to join three times.
SELECT * FROM A
LEFT JOIN (SELECT *, area AS area-A FROM B WHERE area = 'a' ) AS B ON B.id = A.id
LEFT JOIN (SELECT *, area AS area-B FROM B WHERE area = 'b' ) AS C ON C.id = A.id
LEFT JOIN (SELECT *, area AS area-C FROM B WHERE area = 'c' ) AS D ON D.id = A.id
Hope this is what you want.
Basing on the requirement, i think you should try joining on date not ID as below. This will return 3 rows as you are expecting.
select so.date,so.Docname,so.products,so.Quantity,rl.Area,rl.lat,rl.long from Sampling_order AS so
INNER JOIN Representative_locations as rl ON so.Date = rl.Date
WHERE so.ID = 1
Change table names and column names as needed.

first item used by a user

I am writing a query to grab the items that a specific user_id was the first to use. Here is some sample data -
item_id used_user_id date_used
1 1 2012-08-25
1 2 2012-08-26
1 3 2012-08-27
2 2 2012-08-27
3 1 2012-08-27
4 1 2012-08-21
4 3 2012-08-24
5 3 2012-08-23
query
select item_id as inner_item_id, ( select used_user_id
from test
where test.item_id = inner_item_id
order by date_used asc
limit 1 ) as first_to_use_it
from test
where used_user_id = 1
group by item_id
It returns the correct values
inner_item_id first_to_use_it
1 1
3 1
4 1
but the query is VERY slow on a giant table. Is there a certain index that I can use or a better query that I can write?
i can't get exactly what you mean because in your inner query you have sorted it by their used_user_id and and on your outer query you have filtered it also by their userid. Why not do this directly?
SELECT DISTINCT item_id AS inner_item_id,
used_user_id AS first_to_use_it
FROM test
WHERE used_user_id = 1
UPDATE 1
SELECT b.item_id,
b.used_user_id AS first_to_use_it
FROM
(
SELECT item_ID, MIN(date_used) minDate
FROM tableName
GROUP BY item_ID
) a
INNER JOIN tableName b
ON a.item_ID = b.item_ID AND
a.minDate = b.date_used
WHERE b.used_user_id = 1

Fetch DISTINCT records with JOIN of two tables

I have two tables:
builders
b_id fk_c_id
1 1
2 1
3 1
4 1
5 1
6 2
7 2
subbuilders
fk_b_id sb_id
1 2
1 3
1 4
2 5
6 7
and i want Distinct b_id which are not exist in subbuilders table and must have same fk_c_id
I create:
SELECT DISTINCT b.id FROM pms_builder_to_subbuilders bsb
LEFT JOIN pms_builders b ON b.id = bsb.sb_id WHERE b.fk_c_id = '1'
but it show Distinct records from subbuilders.
You can get the desired results with the following query:
SELECT DISTINCT b.b_id FROM builders b LEFT JOIN subbuilders sb ON sb.fk_b_id = b.b_id WHERE b.fk_c_id = '1' AND ISNULL(fk_b_id);
i think you want this query:
SELECT DISTINCT b_ID
FROM builders
WHERE b_ID NOT IN
(SELECT DISTINCT fk_b_id FROM subbuilders)
but it returns
b_ID
========
3
4
5
7
but i didn't understand your statement: must have same fk_c_id. What do you mean by that?
b_id = fk_c_id
if that's the case then there will be no rows returned because only record 1 has the same b_ID and fk_c_id but exists in table subbuilders