简体   繁体   English

select 来自使用特定行值的表

[英]select from table using an specific row values

basically i want to select rows base of specific row values.基本上我想要 select 行的特定行值的基础。 for example i have a table look like this:例如我有一张像这样的表:

id   name    sport      score    gender
1    tom     tennis      5       male
2    mia     football    9       male  
3    maria   tennis      7       male
4    liam    swimming    8       female

i had try this php script:我试过这个 php 脚本:

$query = "select sport , score , gender from sprot_table where id = 1" ;
$res = $con->query($query) ;
$data = $res->fetch_assoc() ;
$sport = $data['sport']  ; $sport = $data['score']  ; $sport = $data['gender']  ; 
$query = "select id , name from sport_table where sport = $sport and gender = $gender and score > $score" ;
$con->query($query) ;

and also tried this sql code:并尝试了这个 sql 代码:

select 
     id , name 
from sport_table where 
sport = ( select sport form sport_table where id = 1 ) and 
gender = ( select gender form sport_table where id = 1 ) and 
score > ( select score form sport_table where id = 1 ) 

i think none of them are optimized.我认为它们都没有优化。 and i'm sure there should be better way to do this maybe this example be so easy but you consider More complicated situations我相信应该有更好的方法来做到这一点也许这个例子很简单但是你考虑更复杂的情况

You can try this query:你可以试试这个查询:

select a.* from
(select * from sport_table) a,
(select * from sport_table where id=1)b
where a.sport=b.sport and a.gender=b.gender and a.score>b.score

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM