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)
Related
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.
I have a table where exists 4 entries like this.
class_type
id type
1 A
2 B
3 M
4 T
and another table where these values are foreign key.
id number id_class_type
1 10 1
2 11 1
3 12 2
4 13 1
5 14 2
6 15 3
7 16 1
8 17 3
So what i want is count(*) and group by id_class_type but with all class_type (1,2,3,4) although there is not present the id 4.
if you want only the class tha match you can use inner join
select a.class_type, count(*)
from class_type a
inner join table2 b on a.id = b.id_class_type
group by a.class_type
otherwise you can use left join
select a.class_type, count(*)
from class_type a
left join table2 b on a.id = b.id_class_type
group by a.class_type
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.
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
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