I have two tables below
t1
-------------------------------------
| id | MaleCnt | FemaleCnt | flag |
------------------------------------
1 20 null 1
2 30 null 1
3 40 null 1
t2
----------------------------
| id | FemaleCnt | flag |
----------------------------
1 20 1
2 30 1
3 40 1
I want to update "FemaleCnt" at table t1 with table t2
(shoud have same id and flag)
I just wrote some query but dont work, so far.
Could you give me some tip??
Just do JOIN & update :
UPDATE t1 INNER JOIN
t2
ON t2.id = t1.id AND t2.flag = t1.flag
SET t1.FemaleCnt = t2.FemaleCnt;
This should work.
UPDATE t1 SET t1.FemaleCnt = t2.FemaleCnt
WHERE t1.id = t2.id AND t1.flag = t2.flag
Related
I need only the ids where the endDate is not greater than today
or all endDates of the id are expired
table 1 t1
id |
---
1
2
3
4
table 2 t2
id | t1_id | endDate
-----------------------
1 | 2 | 2019-01-01
2 | 3 | 2019-01-01
3 | 3 | 2020-01-01
4 | 3 | 2025-01-01
Query
SELECT t1.id ,t2.endDate
FROM table_1 t1
LEFT JOIN table_2 t2 ON t2.t1_id = t1.id
WHERE NOT(t2.endDate > CURDATE())
AND ???
I need this result:
t1.id | t2.endDate
-----------------------
2 | 2019-01-01
If I understand your requirement correctly you can use NOT EXISTS:
SELECT t1.id, t2.endDate
FROM table_1 t1
INNER JOIN table_2 t2 ON t2.t1_id = t1.id
WHERE NOT EXISTS
(
SELECT 1
FROM table_2 t
WHERE t.t1_id = t2.t1_id AND t.endDate > CURDATE()
)
Demo here
this is what I have, 2 tables, and i want to update table #2 with the stock from the first table, but i want to update only the first row of each duplicate.
+----+------+
|code|stock |
+----+------+
|5001| 40 |
|5002| 20 |
|5003| 60 |
+----+------+
+----+------+----+
|code|stock |lot |
+----+------+----+
|5001| | A | < Update this
|5001| | B |
|5002| | C | < Update this
|5003| | D | < Update this
|5003| | E |
+----+------+----+
So here i have the same product on two or more rows, and i want to update only the one from lot:A, lot:c, and lot: D, with the values from the first table.
How can i do this?
You could try using aupdate with join
update table2 t2
inner join (
select code, min(lot) lot
from table2
) t on t2.code = t.code and t2.lot = t.lot
inner join table1 t1 on t2.code = t1.code
set s2.stock = t1.stock
I have a table with some data, and I need to make a query using that table.
I have:
+----+-----------------+--------+--------+
| Id | Name | Parent | Mark |
+----+-----------------+--------+--------+
| 1 | Name 1 | 0 | 0 |
| 2 | Name 2 | 1 | 20 |
| 3 | Name 3 | 2 | 45 |
| 4 | Name 4 | 0 | 50 |
+----+-----------------+--------+--------+
and I need:
+----+-----------------+--------+--------+
| Id | Name | Parent | Mark |
+----+-----------------+--------+--------+
| 2 | Name 2 | Name 1 | 20 |
| 3 | Name 3 | Name 2 | 45 |
+----+-----------------+--------+--------+
How can I run the query in MySQL?
You can get your result by running this query.
Select tablename.*
from tablename t1
inner join tablename t2 on t2.Id = t1.Parent ;
you can do the inner join in the same table and check if the user have parent or not
try something like
select * from marksTbl m inner join marksTbl p on m.Id = p.Parent
You can use the following using a INNER JOIN. Using this solution also exclude all rows without a matching parent.
SELECT t1.Id, t1.Name, t2.Name AS Parent, t1.Mark
FROM table_name t1 INNER JOIN table_name t2 ON t1.Parent = t2.Id
ORDER BY t1.Id ASC
In case you want to see all rows (also the rows without (matching) parents) you can use the following with LEFT JOIN:
SELECT t1.Id, t1.Name, t2.Name AS Parent, t1.Mark
FROM table_name t1 LEFT JOIN table_name t2 ON t1.Parent = t2.Id
ORDER BY t1.Id ASC
You can also use a sub-select instead of a join:
SELECT Id, Name, (SELECT Name FROM table_name t2 WHERE t2.Id = t1.Parent) AS Parent, Mark
FROM table_name t1
WHERE t1.Parent > 0
ORDER BY t1.Id ASC
demo on dbfiddle.uk
I have 2 tables:
T1:
id | name
------ | ------
1 | Bob
2 | John
3 | Joe
T2:
id | T1_id | type
------ | ------ | ------
1 | 1 | call
2 | 1 | email
3 | 1 | fax
4 | 2 | call
5 | 2 | email
6 | 2 | fax
7 | 3 | call
8 | 3 | email
I want to count the number of records in T1 which do not have a record in T2 with a type of 'fax'.
So the answer in this case would be 1 (3|Joe)
Currently I have:
SELECT count(*)
FROM `T1`
JOIN `T2` on `T1`.`id` = `T2`.`T1_id`
WHERE `T2`.`type` != 'fax'
But this is obviously counting all the records which are not 'fax'. I just cant get the logic in my head.
Any help would be appreciated!
A subquery is unnecessary:
SELECT COUNT(DISTINCT t1.id)
FROM t1
LEFT
JOIN t2
ON t2.t1_id = t1.id
AND t2.type = 'fax'
WHERE t2.id IS NULL;
select count(*)
from
(
SELECT t1.id
FROM T1
LEFT JOIN T2 on T1.id = T2.T1_id
GROUP BY t1.id
HAVING sum(T2.type = 'fax') = 0
) tmp
The answers given by Strawberry and juergen d are correct, but for completeness, here's another example using NOT EXISTS. All the queries will have different execution plans, so depending on your data in T1 and T2 YMMV:
SELECT COUNT(*)
FROM `T1`
WHERE NOT EXISTS (
SELECT *
FROM `T2`
WHERE `T2`.`T1_id` = `T1`.`id`
AND `T2`.`type` = 'fax'
)
I'm looking for a way to update some values in a table if they were used in a left join.
I have two tables:
table1:
id | name | age | job
1 | john | 31 |
2 | eric | 25 |
table2:
id | job | inserted
1 | lawyer | 0
2 | dentist | 1
3 | cop | 0
Then I run the query:
UPDATE table1
LEFT JOIN
table2
ON table1.id = table2.id
SET table1.job = `table2.job`
WHERE table2.inserted = 0
But I want to update the rows from table2 that were used in the update so they have inserted = 1. This for two reasons 1) to speed up the join and 2) so I can check which rows of table2 were not used. (The inserts in table2 happen before table1, but the ids in table2 should always be present in table1 if all cron jobs run okay.)
You shouldn't be using a LEFT JOIN, since you only want to update rows in table1 that have a matching row in table2. Try:
UPDATE table1 AS t1
JOIN table2 AS t2 ON t1.id = t2.id
SET t1.job = t2.job, t2.inserted = 1
WHERE t2.inserted = 0
DEMO