How to update single rows in duplicate entries - php

I have two tables t1 and t2
t1->
id line Amt
----------- ----------- -----------
1 1 0
1 2 0
2 1 0
2 2 0
2 3 0
3 3 0
3 4 0
3 5 0
4 2 0
4 3 0
--------------------------
t2->
id amt
----------- -----------
1 500
2 350
3 750
4 400
In this case I need to update t1 with amount from t2. But I need to update only one row for each id on minimum line. I can do it in MSSQL using the following query-
update a set a.amt=c.amt from T1 a inner join (
select id,min(line) line from T1 group by Id) b
on a.id=b.id and a.line=b.line
Inner join T2 c on a.id=c.Id
I want to do it in MYSQL. Is there any idea to do something like this

You need to tweak your syntax remove from clause, move set clause after joins part
update T1 a
inner join (
select id,min(line) line from T1 group by Id
) b on a.id=b.id and a.line=b.line
inner join T2 c on a.id=c.Id
set a.amt=c.amt
DEMO

Related

selecting all row from two tables and one row from last table mysql

I want to select all row from two tables and one row from last table mysql
My table is given below,
tbl_order
order_id order_no
-------- --------
1 1000
2 1001
3 1002
tbl_assign
assign_id order_id central_status
--------- -------- --------------
1 1 1
2 2 1
3 3 1
tbl_unit_status
status_id assign_id status_status
--------- --------- -------------
1 1 Work
2 2 Cutter
3 2 Stitch
4 1 Stitch
From the above 3 table, I want the result as,
order_id order_no assign_id status_status
-------- -------- --------- -------------
3 1002 3 {null}
2 1001 2 Stitch
1 1000 1 Stitch
I have tried the below code,
SELECT * FROM tbl_order o LEFT JOIN tbl_assign a ON a.order_id = o.order_id LEFT JOIN (SELECT * FROM tbl_unit_status u ORDER BY u.status_id DESC LIMIT 1) uu ON uu.assign_id = a.assign_id WHERE a.central_status = 1 ORDER BY a.assign_id DESC
But the result comes as,
order_id order_no assign_id status_status
-------- -------- --------- -------------
3 1002 3 {null}
2 1001 2 {null}
1 1000 1 Stitch
Where am doing wrong. I have tried a lot. Please help me find the answer. Thank you.
try like this:
SELECT o.*,u2.assign_id,u2.status_status FROM tbl_order o
LEFT JOIN tbl_assign a ON a.order_id = o.order_id LEFT JOIN
(SELECT u.assign_id,max(u.status_id) as maxid FROM tbl_unit_status u group by u.assign_id)
uu ON uu.assign_id = a.assign_id
LEFT JOIN tbl_unit_status u2 on u2.status_id = uu.maxid
WHERE a.central_status = 1 ORDER BY a.assign_id DESC
Try this
SELECT * FROM tbl_order o LEFT JOIN tbl_assign a ON a.order_id = o.order_id
LEFT JOIN (SELECT * FROM tbl_unit_status u ORDER BY u.status_id DESC LIMIT 2)
uu ON uu.assign_id = a.assign_id WHERE a.central_status = 1 ORDER BY
a.assign_id DESC
With LIMIT 1 you are only comparing with the last row from tbl_unit_status which has assign_id 1.

Php, MySql join 3 tables ,and calculate SUM

I have 3 tables project, times and expense. I want to join all 3 of them and calculate sum as well as retrieve all the matching records.
Here are my tables:
projects:
id name
=====================
1 First Project
2 Second Project
times:
id project_id hours billed
===================================================
1 1 2.0 1
2 1 3.0 0
3 2 4.30 0
expense:
id project_id amount billed
==================================================
1 1 120.00 0
2 2 35.00 1
3 2 55.00 0
4 2 45.00 0
and here is my query:
SELECT
SUM(t.hours) as total_hours,
SUM(e.amount) as total_amount,
p.name
FROM
`projects` AS p
LEFT JOIN `expense` AS e
ON e.project_id = p.id
LEFT JOIN `times` AS t
ON t.project_id = p.id
WHERE t.billed = 0
AND e.billed = 0
GROUP BY p.id;
But for some reason I cant make it to work, I end up with no records.
The result should look something like:
Name Total Hours Total Expense
==============================================
First Project 3.00 120.00
----------------------------------------------
Second Project 7.30 100.00
----------------------------------------------
You just have the table aliases the wrong way around:
SELECT
SUM(e.hours) as total_hours,
SUM(t.amount) as total_amount,
p.name
FROM
`projects` AS p
LEFT JOIN `expense` e
ON e.project_id = p.id
LEFT JOIN `times` t
ON t.project_id = p.id
WHERE t.billed = 0
AND e.billed = 0
GROUP BY p.id;
Works as expected: http://sqlfiddle.com/#!9/48bfd1/3

Select records from two tables if atleast 4 records in second table

I have 2 tables and i want to fetch data based on below condition:
Table1 has multiple product records.
Table2 contains various Size option for products and can not have more or less than 4 rows.
Now, i want to fetch those products which do not have entry or do not have exact 4 entries.
Table Structure is as below:
Table1
id name color price instock
----------------------------------
1 rice white 1200 1
2 shoe brown 2500 1
3 belt red 5200 1
Table2
id size pid
-----------------
1 5 1
2 10 1
3 4 1
4 15 1
5 7 2
Now Query shall fetch product with ID 2 and 3 as they have records less than 4 and no record resp.
I was using below query to fetch products which have no records in Table2
SELECT p.* FROM `Table1` p LEFT JOIN `Table2` t ON p.id = t.pid WHERE
t.pid IS NULL
SELECT p.id, p.name, p.color, p.price, p.instock, count(t.*)
FROM `Table1` p
LEFT JOIN `Table2` t
ON p.id = t.pid
GROUP BY p.id, p.name, p.color, p.price, p.instock
HAVING count(t.*) < 4

mysql query error matching data

I'm getting an error in sql
I have three tables
T1, t2, t3
in t1
id name
1 a
2 b
3 c
4 d
5 e
in t2
t1_id name
4 sac
2 sau
4 rah
4 seh
1 kaif
5 zah
6 aas
8 ram
in t3
t1_t2_id name count lif_lin
1 Eve 2 no
2 sun 1 no
3 mon 0 no
4 tue 3 no
5 wed 1 no
6 thu 1 no
I want to count the how much t1_id element's exist in t1 respectively of id, t1_id in t1_t2_id
Mean I have category id in t1 and the same category id in t2 table with t1_id and t3 t1_t2_id and i want to count the how many matches found in t2 with the same t1_t2_id
Not sure I exactly understand what you are saying but try:
SELECT count(a.id)
from t1 a
join t2 b on a.id = b.t1_id
join t3 c on a.id = c.t1_t2_id
I think this will work for you (number if t1 ids in t2):
SELECT count(*) from t1,t3 where t1.id=t2.t1_id;

MYSQL/PHP sum with group by

tableA
id B_id C_id
1 1 0
2 1 0
3 0 1
4 0 2
5 0 2
6 0 2
tableB
id amount quantity
1 10 2
tableC
id amount quantity
1 6 1
2 15 3
I have this kind of database and I know it is not structured very well because I only continued this website and I wasn't given much time to restructure the website.
My question is how can i get the total amount of tableB and tableC using a LEFT JOIN of the two tables to tableA. As you can see the quantity in tableB and tableC will make the same number of tableA record. I was able to get each of the transactions amount using this code:
SELECT *
FROM tableA A
LEFT JOIN tableB B ON A.id = B.id
LEFT JOIN tableC C ON C.id = A.id
GROUP BY B.id, C.id
It would return:
id B_id C_id B.id B.amount B. quantity C.id C.amount C.quantity
1 1 0 1 10 2 0 0 0
3 0 1 0 0 0 1 6 1
4 0 2 0 0 0 2 15 3
but now I want to get the total using mysql SUM but GROUP BY cannot be used with the aggregate function SUM so the total should be: 10+6+15 = 31
Is this what you want?
SELECT coalesce(sum(b.quantity), 0) + coalesce(sum(c.quantity), 0)
FROM tableA A LEFT JOIN
tableB B
ON A.id = B.id LEFT JOIN
tableC C
ON C.id = A.id;
This returns the total across the two tables.
EDIT:
If you just want the sum of tables b and c, what is table a for? Is this what you want?
select amountb + amountc
from (select sum(amount) as amountb from tableb) b cross join
(select sum(amount) as amountc from tablec) c;

Categories