duandingcu7010 2014-11-26 13:40
浏览 35
已采纳

在一个MySQL请求中从其他SELECT中的表中进行SELECT?

for now I am doing my thing with this heavy code :

$arrayid = array();
$req = $mysqli->query("SELECT * FROM sc_users_teaching WHERE lesson = '_ADOBE_PHOTOSHOP_' ORDER BY lesson DESC");
while($liste_resultat = $req->fetch_assoc()) {
$ID_USER = $liste_resultat['ID_USER'];
    $req_user = $mysqli->query("SELECT * FROM sc_users WHERE ID_USER = '$ID_USER' LIMIT 0,1");
    $liste_user = $req_user->fetch_assoc();
        $lastconnexion = $liste_user['lastconnexion'];
        $arrayid[$ID_USER] = $lastconnexion;
}
arsort($arrayid);
foreach ($arrayid as $key => $val) {
    //select in sc_users table where ID_USER is $key;
}

So the goal is to select all ID_USER in sc_users_teaching table, then find ID_USER in sc_users and sort them by lastconnexion

I cannot figure how to make the request (UNION or JOIN ?) so if you could help me, I will appreciate.

Thank you !

Solution: "SELECT * FROM sc_users WHERE ID_USER IN (SELECT ID_USER FROM sc_users_teaching WHERE lesson = '_ADOBE_PHOTOSHOP_' ) ORDER BY lastconnexion ASC"

Thanks u_mulder

  • 写回答

1条回答 默认 最新

  • down101102 2014-11-26 13:57
    关注

    So what you need to do is to fetch the ID_USER from the sc_users_teaching and find the ID_USER from sc_users

    So firsty we can select all the items from the table sc_users and finds the ID_USER with the subquery as @u_mulder mentioned in comment which looks like.

    SELECT * FROM sc_users WHERE ID_USER IN (SELECT ID_USER FROM sc_users_teaching WHERE lesson = '_ADOBE_PHOTOSHOP_' ) ORDER BY lastconnexion ASC;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?