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

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

Related

how create correct sql to get only one record with relations?

I have 2 tables:
built_structures:
id | location_id | structure_id
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
ongoing_structures:
id | built_structure_id
1 | 1
2 | 1
Now I need get all data from ongoing_structures where built_structures.location_id = 1 so I expect record id = 1 from ongoing_structures but I have 2 records id 1 and 2 from built_structures
SELECT * FROM ongoing_structures os
JOIN built_structures bs ON (os.built_structure_id = bs.id)
WHERE bs.location_id = 1
How can I get records from ongoing depends from location ?
Update
My mistake thank you for help. Table should be like this
ongoing_structures
id | built_structure_id
1 | 1
2 | 3

SUM() counts row twice and displaying double result

Why SUM() counts row twice and displaying double of actual result in here ?
Here I m trying to count total rows of status that has 0 value from the inv_id table of each student (inv_id.s_id) .
it has to show 4 based on the row number in inv_lst table but here it is showing 8.
If fee.id is GROUP_BY then it shows actual SUM but same student id starts to duplicate.
Please see fiddle - SQL Fiddle
Database Structure
class
id | ttl
===========
1 | One
2 | Two
section
id | ttl
===========
1 | A
2 | B
fee
id | ttl
===============
1 | Annual
2 | Monthly
student
id | ttl | cls | sec
===========================
1 | John| 1 | 1
2 | Paul| 1 | 1
3 | Rina| 2 | 1
sec_fee
id | c_id| s_id| f_id| fee
===================================
1 | 1 | 1 | 1 | 1000
2 | 2 | 1 | 2 | 560
inv_id
id | s_id| ft_id | status
==================================
1 | 1 | 1 | 0
2 | 1 | 2 | 0
3 | 1 | 3 | 0
4 | 1 | 4 | 0
Mysql
SELECT
student.id, student.ttl AS stdt,
cls.ttl AS cls,
sec.ttl AS sec,
GROUP_CONCAT(DISTINCT fee.id, '.', fee.ttl, '-', sec_fee.fee,'<br/>' ORDER BY sec_fee.f_id) AS amnt,
SUM(inv_id.status=0) AS upad,
SUM(inv_id.status=1) AS pad
FROM
student
JOIN
cls ON cls.id=student.cls
LEFT JOIN
sec ON sec.id=student.sec
LEFT JOIN
inv_id ON inv_id.s_id = student.id
LEFT JOIN
sec_fee ON sec_fee.c_id = student.cls
LEFT JOIN
fee ON fee.id = sec_fee.f_id
WHERE
cls.id = 1

MqSql - fetch rows which employee has maximum complaint severity level

I am dealing with a table Employee Complaint which has columns EmployeeId ComplaintSeverity and ComplaintByUser. ComplaintSeverity has four level 0,1,2, and 3.
So the table will look like this ,Example
ComplaintId|EmployeeId|ComplaintSeverity|usr_id
-----------------------------------
1 | 1 | 0 | 3
2 | 2 | 1 | 4
3 | 3 | 0 | 5
4 | 1 | 2 | 4
5 | 4 | 1 | 5
6 | 2 | 2 | 2
7 | 2 | 2 | 4
Any user can complaint employee with any of these level
When client search with severitylevel as 0,
The row should fetch as
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
3 | 3 | 0
for severitylevel as 1,
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
5 | 4 | 1
for severitylevel as 2,
ComplaintId|EmployeeId|ComplaintSeverity
----------------------------
4 | 1 | 2
6 | 2 | 2
EmployeeId 1 has been complained by 2 user with severitylevel 0,2 but his highest severity level is 2. so while searching for 0 level, 1 should not be displayed.
Can anyone help me?
The question was edited after the previous answer was submitted. The following would therefore be more accurate.
SELECT x.*
FROM my_table x
JOIN
( SELECT employeeid
, MAX(complaintseverity) severity
FROM my_table
GROUP
BY employeeid
) y
ON y.employeeid = x.employeeid
AND y.severity = x.complaintseverity
WHERE complaintseverity = 0 -- optional line
ORDER
BY employeeid;
You can try following query.
SELECT *
FROM
(
SELECT cs.`EmployeeId`, MAX(cs.`ComplaintSeverity`) severity
FROM ComplaintSeverity cs
GROUP BY cs.`EmployeeId`
) csdata
WHERE csdata.severity=1
Replace 1 with the severity level you want.

Get other user_ids which have same mid

I want to search user_id of 1 in table below and get other user_ids related to same mid ( in this case mid of 1 & 3)
mid | user_id
1 1
1 2
1 3
2 2
2 3
2 5
3 1
3 5
3 2
The result must be :
mid | user_id
1 1
1 2
1 3
3 1
3 5
3 2
How is it done with MySQL query ?
Assuming I understood correctly, you want to first find all mid values that have a user_id value of 1, then get all user_id values from all those previously gotten mid values.
SELECT mid, user_id from table
where mid IN (SELECT mid FROM table WHERE user_id = 1)
Assuming that you want to get all rows for those mid that matches a user_id this should do what you want:
select * from your_table t1
where exists (
select 1 from your_table t2
where user_id = 1
and t1.mid = t2.mid
)
Sample SQL Fiddle
Result given your sample data:
| MID | USER_ID |
|-----|---------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 3 | 1 |
| 3 | 5 |
| 3 | 2 |

Update with a Min in the table

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/).