I am making a join with two tables, tab_usuarios (users) and tab_enderecos (address).
tab_usuarios structure:
id_usuario
nome
usuario
1
Administrador
admin
2
Novo Usuário
teste
3
Joao Silva
jao
tab_enderecos structure:
id_endereco
id_usuario
cidade
uf
2
1
cidade
SP
20
2
Lorena
SP
22
2
Lorena
SP
24
3
Campinas
SP
28
4
Lorena
SP
I have this simple query which brings me the following result:
Select
u.id_usuario,
u.usuario,
u.nome,
e.id_endereco,
e.cidade,
e.uf
From
tab_usuarios u Left Join
tab_enderecos e On u.id_usuario = e.id_usuario
id_usuario
usuario
nome
id_endereco
cidade
uf
1
admin
Administrador
2
cidade
SP
2
user 2
Novo Usuário
22
Lorena
SP
2
user 2
Novo Usuário
20
Lorena
SP
3
jao
Joao Silva
24
Campinas
SP
4
teste
fabio
28
Lorena
SP
What I want is, for example, for id_usuario = 2, I only want to bring the id_endereco = 20, which is the first address that have been inserted on the database.
I tried with min and a couple others.
This should do it, assuming you have MySql 8.0 and not some ancient 5.x version:
SELECT *
FROM (
SELECT u.id_usuario, u.usuario, u.nome, e.id_endereco, e.cidade, e.uf,
row_number() over (partition by u.id_usuario order by e.id_endereco) rn
FROM tab_usuarios u
LEFT JOIN tab_enderecos e On u.id_usuario = e.id_usuario
) t
WHERE rn = 1
See it work here:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=c506baf8157f82390bb335d074e7614c
Related
i need help find who fail in exam & resit and pass the exam only,
heres the code:
select STUDENT_ID,EXAM_ID,SCORE,PASS_THRESHOLD,s.NAME , c.NAME as Course_name, EXAM_DT,
case
when SCORE>=PASS_THRESHOLD then 'PASS'
else 'Fail'
end as Flag
from exam_submission es
left join student s on es.STUDENT_ID = s.ID
left join exam e on es.EXAM_ID = e.ID
left join course c on e.COURSE_ID = c.ID
heres the result:
STUDENT_ID EXAM_ID SCORE PASS_THRESHOLD NAME Course_name EXAM_DT Flag
1 3 88 65 Anthony Data Mining 2019-12-17 PASS
1 5 71 70 Anthony Statistic 2019-12-19 PASS
2 1 53 55 Sisca Machine Learning2019-12-17 Fail
2 3 77 65 Sisca Data Mining 2019-12-17 PASS
2 4 85 63 Sisca Data Science 2019-12-18 PASS
2 1 60 55 Sisca Machine Learning2020-01-08 PASS
I need find like this:
2 1 53 55 Sisca Machine Learning2019-12-17 Fail
2 1 60 55 Sisca Machine Learning2020-01-08 PASS
Possibly using a query like below.
this is using your query as input.
Also we have assumed that it is not possible to have a student have (PASS, FAIL) for a student on same exam on two years chronologically.
; with inputdata as
(
select STUDENT_ID,EXAM_ID,SCORE,PASS_THRESHOLD,s.NAME , c.NAME as Course_name, EXAM_DT,
case
when SCORE>=PASS_THRESHOLD then 'PASS'
else 'Fail'
end as Flag
from exam_submission es
left join student s on es.STUDENT_ID = s.ID
left join exam e on es.EXAM_ID = e.ID
left join course c on e.COURSE_ID = c.ID
)
select * from Inputdata I
join
( select student_id, exam_id from
inputdata
group by student_id, exam_id
having count(distinct flag)=2
)T on I.student_id=T.student_id and I.exam_id=T.exam_id
order by exam_dt asc
I have got 3 tables on my database. Lets assume they are named as ACC, POS, CON.
At ACC table i have got ID and NAME columns. At POS table, there is LAT and LONG columns and at CON table, AccID and FrID columns.
to give an example.
ACC / POS / CON
ID NAME / ID LAT LONG / AccID FrID
1 Mike 1 10 15 1 2
2 Bob 2 20 25 1 4
3 Jack 3 18 21 2 3
4 Rocky 4 37 45 2 1
This is the data from my 3 tables. Now i want to select all LAT and LONG values from POS and NAME values from NAME where AccID in FrID.
To be more specific, i want to check for Mike's friend's Lat Long. At Con table there is two friends of Mike, ID=2 and ID=4 so i want to get a table like.
ACC_NAME LAT LONG
Bob 20 25
Rocky 37 45
can you give me a query example for this problem. Thank you.
You can try by using INNER JOIN. Here is full Query:-
SELECT ACC.Name, POS.LAT, POS.LONG FROM CON
INNER JOIN ACC ON CON.FrID = ACC.ID
INNER JOIN POS ON POS.ID = ACC.ID
WHERE CON.AccID = 1
Explanation Here
Get All friends of Mike By
SELECT * FROM CON
WHERE CON.AccID = 1 -- Mike Account Id
Get Friends Name by Join with Acc Table
SELECT ACC.Name FROM CON
INNER JOIN ACC ON CON.FrID = ACC.ID
WHERE CON.AccID = 1
Get Lat ang Long by Join with POS Table
SELECT ACC.Name, POS.LAT, POS.LONG FROM CON
INNER JOIN ACC ON CON.FrID = ACC.ID
INNER JOIN POS ON POS.ID = ACC.ID
WHERE CON.AccID = 1
I have a table to only I have 3 levels in structure example:
1 his son is 2 and 2 have a son is 4 and 6
3 is father and his son is 5
and 1 have a other son is 7
4 doesn't have son because is a rule of the structure.
well my table is this:
|Id_Father| |Id_Son|
1 2
2 4
3 5
2 6
1 7
Only I want to bringme with my query every father to have one son, this is part of my query:
SELECT R.* FROM getName R where not EXISTS (SELECT 1 from Estructura R2 where R.id = R2.Id_Son) And Exists (SELECT 1 from Estructura R2 where R.id = R2.Id_Father and not exists (SELECT 1 from Estructura R3 where R2.Id_Son = R3.Id_Father ))
and I get this with my query :
1 and 3 but I want only get the 3.
try this one:
select id_father
from Estructura
group by id_father
having count(distinct id_son) = 1
i've tables like
personal info:
uid name location relation
12 ario 32 1
13 arvin 32 4
Professional Info
uid inst pos
12 SER admin
13 Wipro data analyzer
now, how to get the uid who works in Wipro, with relation of 4 (which is 13)
select * from `personal info` p inner join `Professional Info` pi
on p.uid = pi.uid
where p.relation = 4
I got two tables.
A dealer table (id, name, etc.) and an event table (id, did(dealerid), eid(fb eventid), venue, etc.) where I save the dealerid, eventid, eventurl and date from facebook.
I can get all data with this sql statement:
SELECT * FROM dealer as t_d INNER JOIN events AS t_e
ON t_d.id = t_e.did;
This works fine.
1 Laden Dortmund Stra 12 45525 Dortmund DE www.google.de 0201-123456 laden#dortmund.de 300 400 1 1 416364098428245 https://www.facebook.com/events/416364098428245/ 2013-01-28
2 Laden Unna Wurststr. 123 45130 Unna DE www.bing.de 0222-11223344 laden#unna.de 400 276 2 2 145457548940302 https://www.facebook.com/events/145457548940302/ 2013-03-08
2 Laden Unna Wurststr. 123 45130 Unna DE www.bing.de 0222-11223344 laden#unna.de 400 276 3 2 405092472902921 https://www.facebook.com/events/405092472902921/ 2013-04-26
As you can see, I get two rows for the dealer id 2, because there are two events listed.
How can I get the event, which is the closest to this date (today).
Later, there will be like 20 Dealer and for each one at least 3-4 events.
A hint would be nice, thank you!
UPDATE:
This is the solution!
SELECT t_d.*, t_e.*, DATE_FORMAT(t_e.date,'%d.%m.%Y') as date
FROM dealer as t_d
INNER JOIN events AS t_e
ON t_d.id = t_e.did
INNER JOIN
(
SELECT did, MIN(date) minDate
FROM events
GROUP BY did
) e ON t_e.did = e.did AND
t_e.date = e.minDate
But is it somehow possible to display ALL dealers, even if no event exists for the dealer?
SELECT t_d.*, t_e.*
FROM dealer as t_d
INNER JOIN events AS t_e
ON t_d.id = t_e.did
INNER JOIN
(
SELECT did, MAX(date) maxDate
FROM events
GROUP BY did
) e ON t_e.did = e.did AND
t_e.date = e.maxDate