开发者

sql query: select people with same name ascending by age

开发者 https://www.devze.com 2023-01-06 21:06 出处:网络
Have 2 table 1st {id_city,name } 开发者_如何学Go 2nd table {id_ctz,name,sname,age,id_city} need selected full information (name(city),name,sname,age) people with same name ascending by age?

Have 2 table

1st {id_city,name } 开发者_如何学Go
2nd table {id_ctz,name,sname,age,id_city}

need selected full information (name(city),name,sname,age) people with same name ascending by age?

Have tried:

select city.name,citizen.name,age 
from citizen,city where city.id_city = citizen.id_city and citizen.name = '%s' 
order by age asc 

input variable is name


This is the best I could do with the (lack of) information given.

SELECT *
FROM 1st a
JOIN 2nd b ON a.id_city=b.id_city
WHERE a.name = 'same'
ORDER BY b.age ASC

Note: Don't actually use the * as your output columns. Use the actual names.


I'm guessing that you want all rows to be returned, and you want people with some name first, sorted by age. Afterwards you want all other rows. This query will do that:

SELECT city.name, citizen.name, citizen.age 
FROM citizen
JOIN city ON city.id_city = citizen.id_city
ORDER BY citizen.name <> 'some_name', citizen.age


select c2.name AS CityName,
c1.name AS CitizenName,
c1.age  AS CitizenAge
from citizen as C1 
inner join city as C2 on C1.id_city = c2.id_city
where c1.name = 'frank' 
order by c1.age asc 


Is what you're looking for really help with how to pass a parameter to a query? In that case, your problem is ='%s'. You need to remove the single quotes. In a TSQL stored procedure, it would be = @s instead. MySQL I'm not sure about (you've got both tags), and if you're using something other than stored procedures to do a parameterized query, it would depend on the data provider you're using.

0

精彩评论

暂无评论...
验证码 换一张
取 消