I have table like this:
ID NAME
1 juan
2 pedro
3 jose
4 lucas
5 antoni
I need show result like this:
1 juan
3 jose
5 antoni
How should I proceed to show every record in a sequence of 2 ?
Thank you
Use Modulo(%) operator
Select * from yourtable Where ID % 2 = 1
This considers ID is sequential
SELECT * FROM
(
SELECT ID,NAME,ROW_NUMBER()OVER(ORDER BY ID ASC)as ROW
FROM TABLE
) as A
WHERE ROW % 2 = 0
Related
I want to find table entry that appears more than once with certain value
Name | ID
==================
Peter 1
James 2
Peter 2
James 2
I want to select entries where ID is both 1 and 2, in this example only Peter appears twice with value 1 and 2 while James appear twice but ID of James is not 1 and 2
Is there operator for this kind of query?
You can do:
select name
from t
where id in (1, 2)
group by name
having count(distinct id) = 2;
I have a table name player_history containing history of player. in this table having column player_id, Final_position,meeting_code,race_no and beaten_time. If a player stood a first or second position the time will be same there are meeting code one day and in each meeting code there are maximum 10 races.
I want to select those records where 1st and second position beaten time are not same.
player_id Meeting_Code race_no final_position beaten_time
1 0001 1 1 2
2 0001 1 2 2
1 0001 2 1 5
2 0001 2 2 6
... so on
Output should be:
player_id Meeting_Code race_no final_position beaten_time
1 0001 2 1 5
2 0001 2 2 6
Also if it is not correct I want to update records of first position only.
Let table name be Test. Try to put Self Join or something like this:
Select t1.player_id t1.Meeting_Code t1.race_no t1.final_position t1.beaten_time
From Test t1
LEFT JOIN Test t2 ON t1.beaten_time != t2.beaten_time.
Try like this. It might work.
Check this it will work
SELECT *
FROM table_name
GROUP BY player_id
HAVING count(beaten_time) = 1;
Try It:
select b.player_id,b.meeting_code,b.race_no,b.final_position,b.beaten_time
from player_history a,player_history b
where a.race_no = b.race_no and a.beaten_time != b.beaten_time;
id - in_id - nat_id
1 1 1
2 1 3
3 3 3
4 2 1
Is it possible to select with mysql only the values in the above table which are the same across the 3 columns, ie return 1 and 3?
Or is this kind of filter only possible post query with php?
Thanks,
John
This simple query should work for you:
SELECT id
FROM your_table
WHERE id = in_id
AND nat_id = in_id
;
Check example at SQLFiddle: SQLFiddle Example
I have this query but apparently it can loop and crash the server.
SELECT neveras.Panel, contactos.Email FROM neveras, contactos
WHERE neveras.Alarma = 1 And Estado <> 1
And contactos.Sensor
LIKE CONCAT('%,',(Select Usuario FROM neveras where Alarma = 1),',%')
Table neveras:
Id|Panel|Usuario|Alarma|Estado
1 uno 1 1 2
2 dos 1 2 1
3 tres 2 2 1
4 cuatro 2 2 1
5 cinco 3 2 1
Table Contactos:
Id |Email |Nombre |Sensor
1 uno#uno nombre1 1,3,5
2 dos#dos nombre2 2,4
This table has this structure to avoid repeating values
I appreciate your help.
This is a bit of a guess as I am not 100% sure what you are trying to achieve, but give this a try -
SELECT neveras.Panel, contactos.Email
FROM neveras
INNER JOIN contactos
ON FIND_IN_SET(neveras.Usuario, contactos.Sensor)
WHERE neveras.Alarma = 1
AND neveras.Estado <> 1
As Mosty pointed out it would definitely help if you posted an example of what you expect in the result.
Further to that, you should move the multiple values in your Sensor field to a many-to-many table (contactos_id, sensor_id). There is no way for the optimizer to do anything clever with your comma sparated list so any filtering or joining on that field will be very inefficient.
I have this data on a table :
id name field
0 marco attack
1 andrea defense
2 luca medium
3 ernesto defense
4 vittorio medium
5 manuele attack
i need to order as field. BUT, the priority list order (for my example) should be defense-medium-attack.
so it must return :
andrea, ernesto, luca, vittorio, marco, manuele.
How can do it? bye
You should store the fields in a separate table and give them a sort order. Then you can join to that table.
As well as allowing you to sort efficiently, it also makes the table structure more relational - which is good.
id field sort
1 defense 1
2 medium 2
3 attack 3
id name field
0 marco 3
1 andrea 1
2 luca 2
3 ernesto 1
4 vittorio 2
5 manuele 3
select p.name,
ps.field
from players p
join playersort ps
on p.field = ps.id
order by ps.sort
SELECT
X.id,
X.name,
X.field
FROM (
SELECT id,
name,
field,
CASE field WHEN 'defense' THEN 1
WHEN 'medium' THEN 2
WHEN 'attack' THEN 3
END AS SortValue
FROM MyTable) AS X
ORDER BY X.SortValue