Update with a Min in the table - mysql

I want to update a Column in a Table, based on the minimum of another column of the same table.
eg.
JobPositonId | JobPositonName | JobDescriptionId | ContactId
1 | 1 | 1 | 1
2 | 1 | 1 | 0
3 | 1 | 1 | 0
I want to update ContactId to be 1, if it is 0 and where JobPositionId is the lowest.

I think this should work:
update jobTable
set contactid = 1
where jobPostitionId in (select pos from (select min(jobPositionId) as pos from jobTable where contactId = 0) as subtable);
It's kind of a hack similar to what's described here (http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/).

Related

Find the sum of DATETIME differences within a table?

I have the following table:
---------------------------------
| id | class_id | time | status |
---------------------------------
| 1 | 1 | <> | 1 |
---------------------------------
| 2 | 2 | <> | 1 |
---------------------------------
| 3 | 1 | <> | 0 |
---------------------------------
| 4 | 2 | <> | 0 |
---------------------------------
I want a query that will see that the first row has class_id = 1 and status = 1. It will then look for the next row with class_id = 1 and status = 0, and find the time difference between the two (time is DATETIME).
At the end of all this, it will return me a sum of all time differences (i.e. (row 1 - row 3) + (row2 - row4)).
How is this possible? In generalisation, the question is about getting an aggregate total of differences between rows in a table, based off a condition.
For every status 0 record we search the latest status 1 record. This is from all previous status 1 records take the latest.
select
class_id,
sum
(
timestampdiff
(
second,
(
select s1.time
from mytable s1
where s1.status = 1
and s1.class_id = s0.class_id
and s1.id < s0.id
order by s1.id desc limit 1
),
s0.time
)
) as diffsecs
from mytable s0
where status = 0
group by class_id;

Intersection of two select

I have this table :
+----+-----------+-------+
| id | client_id | is_in |
+----+-----------+-------+
| 1 | 1 | 0 |
+----+-----------+-------+
| 2 | 2 | 0 |
+----+-----------+-------+
| 3 | 1 | 1 |
+----+-----------+-------+
| 4 | 2 | 1 |
+----+-----------+-------+
| 5 | 3 | 1 |
+----+-----------+-------+
| 6 | 3 | 1 |
+----+-----------+-------+
| 7 | 1 | 0 |
+----+-----------+-------+
| 8 | 4 | 0 |
+----+-----------+-------+
| 9 | 4 | 0 |
+----+-----------+-------+
And I need to get the number of clients that have 'is_in' equal to 1 at least one time and that have never had 'is_in' equal to 0 (in this case one the client_id 3).
To do so, I made two queries:
SELECT client_id FROM foo WHERE is_in = 1;
and
SELECT client_id FROM foo WHERE is_in = 0;
And I planned to to an INTERSECT between them so I can get the common entries between the two selects so I just need to do "number of clients with is_in = 1" - "count(of the result of the intersect)".
But INTERSECT can't be used with MYSQL, is there an alternative to INTERSECT that work in this case or a simpler way to get what I need (I'm feeling that I'm doing complicated for nothing).
Thank you.
SELECT id, client_id FROM foo WHERE is_in = 1 AND client_id NOT IN (SELECT client_id FROM foo WHERE is_in = 0)
Or, if you need only the client number:
SELECT DISTINCT client_id FROM foo WHERE is_in = 1 AND client_id NOT IN (SELECT client_id FROM foo WHERE is_in = 0)
what you could so sum them up and get the smallest value, grouped by client id and have the result exclude any of those that had a zero. Try this:
SELECT COUNT(client_id)
FROM foo
GROUP BY client_id
HAVING SUM(is_in) > 0 && MIN(is_in) > 0

How to update order field's value sequentially for specified multiple id only?

I have following table in MySQL.
id | aname | orderNum | parentId
1 | A | 2 | 1
2 | B | 1 | 1
3 | C | 4 | 2
4 | D | 1 | 2
5 | E | 3 | 2
6 | F | 2 | 2
7 | G | 3 | 1
I want to update orderNum field for following:
Set orderNum=1 when id=3 and parentId = 2
Set orderNum=2 when id=7 and parentId = 2
Set orderNum=3 when id=5 and parentId = 2
Set orderNum=4 when id=6 and parentId = 2
In short I would like to set orderNum in increment order for ids (3,7,5,6). I will receive id in (3,7,5,6) order and then I want to update orderNum field in same sequence.
Can anybody help? How can I create an Update query for this?
I want to do this with MySQL.
Note: please see updated question
here is the sql query to do what you want.
Update TABLENAME set `orderNum`= `id`;
Use FIELD() to get the index of the id in our sequence of ids
update your_table
set ordernum = field(id, 3,7,5,6)
where id in (3,7,5,6)
and parentId = 2

how to write this self join based on three columns

Hello there I have a following table
------------------------------------------
| id | language | parentid | no_daughter |
------------------------------------------
| 1 | 1 | 0 | 2 |
------------------------------------------
| 1 | 1 | 0 | 2 |
------------------------------------------
| 2 | 1 | 1 | 1 |
------------------------------------------
| 2 | 2 | 1 | 1 |
------------------------------------------
| 3 | 1 | 1 | 0 |
------------------------------------------
| 3 | 2 | 1 | 0 |
------------------------------------------
| 4 | 1 | 2 | 0 |
------------------------------------------
| 4 | 2 | 2 | 0 |
------------------------------------------
| 5 | 1 | 2 | 0 |
------------------------------------------
| 5 | 2 | 2 | 1 |
-----------------------------------------
| 5 | 1 | 4 | 1 |
------------------------------------------
| 5 | 2 | 4 | 1 |
------------------------------------------
Scenario
Every record has more than one rows in table with different language ids. parentid tells who is the parent of this record. no_daughter columns tells against each record that how many child one record has. Means in Ideal scenario If no_daughter has value 2 of id = 1 , it means 1 should be parentid of 2 records in same table. But If a record has more than one exitance with respect to language, it will be considered as one record.
My Problem
I need to find out those records where no_daughter value is not correct. It means if no_daughter is 2, there must be two records whoes parentid has that id. In above case record with id = 1 is valid. But record having id = 2 is not valid because the no_daughter = 1 but actual daughter of this record is 2. Same is the case with id=4
Can any body tell me how can I find these faulty records?
Updated after answers
Ken Clark has and shola has given answer which return same result for example shola query is
SELECT DISTINCT
id
FROM
tbl_info t
INNER JOIN
(SELECT
parentid,
COUNT(DISTINCT id) AS childs
FROM
tbl_info
GROUP BY parentid) AS parentchildrelation
ON t.id = parentchildrelation.parentid
AND t.no_daughters != parentchildrelation.childs
This query is returning those ids who have been used as parentid somewhere in table but having wrong no_daughter values. But not returning ids that has value in no_daugter columns but have not been used as parentid any where in table. For exampl id = 5 has no_daughter = 1 but it is not used as parentid in table. So it is also a faulty record. But above query is not capturing such records.
Any help will be much appreciated.
Try this:
SELECT DISTINCT
id
FROM
tbl_info t
Left JOIN
(SELECT
parentid,
COUNT(DISTINCT id) AS childs
FROM
tbl_info
GROUP BY parentid) AS parentchildrelation
ON t.id = parentchildrelation.parentid
Where t.no_daughters != parentchildrelation.childs
Try this:
SELECT id FROM tinfo t inner join
(SELECT parentid, COUNT(distinct language ) as childs FROM tinfo group by parentid) as summary
on t.id=summary.parentid and t.no_daughters!= summary.childs
try this
Select Distinct * From tablename t
Left Join
(
Select COUNT(t1.Id) Doughter,t1.parentid,t1.language From tablename t1 Group By t1.parentid,t1.language
)tbl
On t.id=tbl.parentid And tbl.language=t.language And t.no_daughter<>tbl.Doughter

Update rows on the basis of another row in the same table

I've got a table which looks something like this
id |game_id| player_id| winner
----------------------------
1 | 1 | 1 |
2 | 1 | 2 |
3 | 1 | 3 |
4 | 1 | 4 |
i have a game_id and winner id , i wanna update winner as 1 and loser as 0;
UPDATE tournament SET winner = '1' WHERE game_id= 1 AND player_id = 1
then
UPDATE tournament SET winner = '0' WHERE game_id= 1 AND player_id != 1
OUTPUT
id |game_id| player_id| winner
----------------------------
1 | 1 | 1 | 1
2 | 1 | 2 | 0
3 | 1 | 3 | 0
4 | 1 | 4 | 0
can anyone tell me how it can be done using single query
UPDATE tournament SET winner = IF(player_id = 1, '1', '0') WHERE game_id= 1
Query
SQLFIDDLEExample:
UPDATE tournament
SET winner = CASE WHEN player_id = 1
THEN '1'
ELSE '0'END
WHERE game_id= 1