Totally out of ideas here, could be needing a simple solution.
Basically my desired query is :
SELECT * FROM table WHERE id = 3,4
I want to select only the row which has ID 3 and 4, or maybe name "andy" and "paul"
Thank you very much for the answer
Try or:
WHERE id = 3 or id = 4
Or the equivalent in:
WHERE id in (3,4)
Try this -
select * from table where id in (3,4) or [name] in ('andy','paul');
Related
I have a table and now I want to define which mentor has all his interns with marks "5".
I tried to add this:
SELECT Mentor_lname FROM Table WHERE Mentor_lname = ALL (SELECT Mentor_lname
FROM Table WHERE mark = 5));
But it doesn't help, I have already spent a few hours to get a proper result. Maybe I use "ALL" incorrectly?
[Table]
The expected result is Natarov.
Assuming 5 is the maximum possible mark, you can just check whether MIN(mark) == 5 for each mentor.
SELECT Mentor_lname
FROM Table
GROUP BY Mentor_lname
HAVING MIN(mark) == 5
if I understand correctly you want to do this :
select *
from tablename
where Mentor_lname IN (
SELECT Mentor_lname
FROM tablename2
WHERE Practise_result.mark = 5
);
but if only 1 table is involved then :
select * from tablename where mark = 5
I have a table that looks something like the following:
id
id_player1
id_player2
1
2
1
2
3
2
I would like to know if there is a way to make it something like:
id
player_selected
opponent
1
2
1
2
2
3
I have got my basic MySQL query:
SELECT * FROM `pairings`
WHERE id = $id AND id_player1 = $idPlayer OR id_player2 = $idPlayer
I'm not used to make much queries for mysql, and I don't much about them and if this is possible.
I'd do it this way:
SELECT id, id_player1 AS player_selected, id_player2 AS opponent
FROM pairings
WHERE id_player1 = $idPlayer
UNION
SELECT id, id_player2, id_player1
FROM pairings
WHERE id_player2 = $idPlayer;
This has an advantage that it makes use of indexes on both id_player1 and id_player2.
(Defining column aliases with AS is not needed on the second SELECT because the aliases defined in the first SELECT take priority anyway.)
I can to advice next query:
select
id,
if (id_player1 = $idPlayer, id_player1, id_player2) as player_selected,
if (id_player1 <> $idPlayer, id_player1, id_player2) as opponent
from pairings
where id_player1 = $idPlayer or id_player2 = $idPlayer
;
This query also can use index based on two fields (id_player1, id_player2)
Here live SQL
I have a sample SQL statement that says:
SELECT * from users WHERE id = 2 OR id = 5 OR id = 7
What I would like is to avoid repeating id each time in the where clause. Is there a shortcut for this in MySQL that will allow me to mention the id only once?
Yes, the IN clause
SELECT * from users WHERE id IN(2,5,7);
if these Ids you are using in the comparison come from another table you can even do
SELECT * FROM users WHERE id in (SELECT other_id FROM other_table WHERE somecondition)
e4c5 gave you the answer you needed, but here is something else you can do with IN:
select * from users where 'steve' IN (users.fname, users.lname)
I have Query like this
Select * from customers where id = 123 and name like '%tester%';
If id : 123 and name : "tester" doesn’t exist in table i should fetch other rows with name "tester" discarding condition "id". if it exists fetch row for that id and name.
Guys i know this is can be handled in program, i want this to be done in my Query, can you please STOP DOWN VOTING and give me the solution if you know!!!
You can try something like this:
SELECT *
FROM CUSTOMERS
WHERE ( ID = 123
AND NAME LIKE '%tester%' )
OR ( NAME LIKE '%tester%'
AND NOT EXISTS (SELECT *
FROM CUSTOMERS
WHERE ID = 123
AND NAME LIKE '%tester%') )
You can find a working example on SQL Fiddle.
SELECT * FROM customers WHERE (id = 123 AND name like '%tester%') OR (name LIKE '%tester%') LIMIT 1;
It should work then
I gotcha here, check it
Select * from customers where id = 3 or name like '%tester%'
order by id=3 desc limit 1;
See you will get all rows that have either id= 3 or name is like tester. From there you will order then by the boolean value (1 if true) by if they == 3. Limiting this to 1 result will get you only the best response.
Likewise if you want to get all results you could remove the limit, assume the top one is the best result. If the first results id is not = 3 then you could say that all the results are just best matches.
I have a table 'movies' with three Columns: 'id', 'master_id' and 'searchMe' (simplified). I have another Table 'temp_ids' with a single column: 'id'. It is a temporary table, but I don't think that matters.
When I make a query on my table 'movies' like
SELECT `id`, `master_id` FROM 'movies' WHERE searchMe = '1';
I get a multi column result. Now I want to insert every id and every master_id into the 'temp_ids'-Table, but one at a time. So if my result is
id_1 | master_1
id_2 | master_2
id_3 | NULL
I want my temp_ids to look like
id_1
master_1
id_2
master_2
id_3
So I want to convert every single column in the result into its own row. How can I do that in an elegant way?
I know I can do it in multiple queries, searching for id and master_id separatly, and I know I can solve that problem with PHP or so. But I would prefer it to solve that problem in a single mysql-query, if such a thing is possible.
I made a sqlfiddle for this:
http://sqlfiddle.com/#!2/b4a7f/2
To SELECT the data you can use a UNION ALL for this:
SELECT `id`
FROM movies
WHERE searchMe = 1
union all
SELECT `master_id`
FROM movies
WHERE searchMe = 1
and master_id is not null
see SQL Fiddle with Demo
Doing it this way, you cannot distinguish between what value comes from each column, so you can always add an indicator, this will give you two columns but then you know where the data came from:
SELECT `id`, 'id' type
FROM movies
WHERE searchMe = 1
union all
SELECT `master_id`, 'master'
FROM movies
WHERE searchMe = 1
and master_id is not null
Then you would just use this query to INSERT INTO temp using this SELECT
It would be like this
INSERT INTO temp_ids(id)
SELECT id
FROM
(
SELECT id
FROM FirstTable
UNION
SELECT master AS id
FROM SecondTable
) t