I need to create a SELECT query that would return rows where at least one of X columns is of certain value.
id name teamid player1 player2 player3 ... player12
1 Jane 2 43 46 12 ... 36
2 Mathew 6 12 56 18 ... 42
3 Shaun 8 53 55 12 ... 62
4 Jane 1 1 53 19 ... 34
5 Eugene 3 23 34 13 ... 44
In this table i have 14 columns (id, name and player1 to player12) and want to SELECT row of specific teamid, let's say teamid='2', and return the name of column that contains for example number 12 in this row.
Is this even possible with MySQL?
If I understand, then:
select t.*,
(case when player1 = 12 then 'player1'
when player2 = 12 then 'player2'
. . .
when player12 = 12 then 'player12'
end) player_with_12
from t
where t.teamid = 2;
You might want to revisit your data structure, so you have one row per team and player.
Related
I have a table as follows:
ID PID TID CID DID IsTrue
1 43 1 2 621 0
2 43 1 2 621 1
3 45 2 3 621 1
4 46 2 3 621 0
and I need to find the count of PID,CID and with the where condition DID=621 along with the other fields.
how can I do that?
Thanks.
Try this
Select count(pid) from table where did = 121 group by pid.
Table:
pk fk price
1 1 23
2 1 12
3 1 3
4 2 53
5 2 75
6 3 95
7 3 113
8 3 63
9 3 73
10 3 93
11 4 113
11 4 150
11 4 105
In the above table:
How to find out the lowest price based on it's common fk value.For example: lowest price for fk=1 is 3, for fk=2 is 53, for fk=3 is 63 and for fk=4 is 105.
I want a single SQL statement which could find lowest price for each common fk value.
You just want a basic aggregate per group.
select fk, min(price)
from your_table
group by fk;
I would like to compare three columns in two different tables by joing through unique identifier.But my for single identifier there are multiple rows will be returned.
Example:
Table A
Identifier Flag1 Flag2 Flag3
1 56 36 46
1 89 65 33
1 56 89 22
1 11 89 65
Table B
Identifier Flag 1 Flag2 Flag3
1 56 36 46
1 89 65 33
1 56 89 22
1 10 89 65
Now i would like to compare these two tables based on Identifier 1 , can you please help me out if all the column values are matching i need update the flag.Thanks in advance
I think below code will help,
below code will update flag of table A for records whose column values are same as that of table b
update a
set someflag = 1
where exists
(
select * from B
where b.flag1 = a.flag1
and b.flag2 = a.flag2
and b.flag3 = a.flag3
)
I have this table:
id_user id_user2
1 54
1 53
1 53
1 54
1 54
1 55
2 23
2 23
2 20
2 21
2 25
2 25
And i would like to count, how many each of id_user have relationship with id_user2. Output should be:
id_user id_user2 result
1 54 3
1 53 2
1 55 1
2 23 2
2 20 1
2 21 1
2 25 2
You have to use group by clause
select id_user,id_user2, count(1) as result
from userstab
group by id_user,id_user2
try this query
select id_user,id_user2,count(id_user2) as result
from TABLE_NAME group by id_user2
On a MySQL database, I have the table below
package_content :
id | package_id | content_number | content_name | content_quality
1 99 11 Yellow 1
2 99 22 Red 5
3 101 11 Yellow 5
4 101 33 Green 5
5 101 44 Black 5
6 120 11 Yellow 5
7 120 55 White 5
8 135 66 Pink 5
9 135 99 Orange 5
10 135 11 Yellow 5
and i am looking a possibility to make search queries on it:
I would like to select the package_id where content_number could be 11 AND 22 (In this case it should select only package_id 99
I really don't know if it's possible in SQL since the statement AND will always results as false. If i use the statement OR i also get the package_id 99, 101, 120, 135 and that's not what i want.
Maybe my table is not well designed too, but any suggestions would help!
Thanks in advance
Edit
I added the content_quality column
I used the sql query from juergen, works very well
select package_id
from package_content
where content_number in (11,22)
group by package_id
having count(distinct content_number) = 2
My last question is how could i now add another criteria : Select the package_id where content_number is 11 and 22 and content_number 11 has content_quality 1
Edit 2:
For the 2nd question i use now this query. Thanks to both of you who helped me! :)
SELECT *
FROM (
SELECT package_id
FROM package_content
WHERE
(content_number=11 AND content_quality > 1)
OR (content_number = 33 AND content_quality = 5)
OR (content_number = 44 AND content_quality =5 AND content_name like 'Black')
GROUP BY package_id
HAVING count( DISTINCT content_number) = 3
)t1
LEFT JOIN package_content ON package_content.package_id = t1.package_id
This will output
id | package_id | content_number | content_name | content_quality
3 101 11 Yellow 5
4 101 33 Green 5
5 101 44 Black 5
You need to group by the package_id and then use having to perform an aggregate function over the grouped data
select package_id
from package_content
where content_number = 22
or
(
content_number = 11 and content_quality = 1
)
group by package_id
having count(distinct content_number) = 2
You could query with a self join for that:
SELECT DISTINCT package_id
FROM package_content a, package_content b
WHERE a.package_id = b.package_id
AND a.content_number = 11 AND b.content_number = 22
Edit: For your second question: Just add that to the query. The package_content renamed to a is responsible for the content_number 11. Therefore you can ask, wether a has content_quality 1:
SELECT DISTINCT package_id
FROM package_content a, package_content b
WHERE a.package_id = b.package_id
AND a.content_number = 11 AND b.content_number = 22
AND a.content_quality = 1