简体   繁体   中英

SELECT in a table from an other SELECT in one MySQL request?

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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