简体   繁体   English

从一个MySQL请求中的另一个SELECT中选择一个表中的SELECT?

[英]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 因此,目标是在sc_users_teaching表中选择所有ID_USER ,然后在ID_USER中找到ID_USER sc_users对其进行lastconnexion

I cannot figure how to make the request (UNION or JOIN ?) so if you could help me, I will appreciate. 我无法弄清楚如何发出请求(UNION或JOIN吗?),所以如果您能帮助我,我将不胜感激。

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" 解决方案: "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 谢谢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 所以,你需要做的是获取ID_USERsc_users_teaching并找到ID_USERsc_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. 因此,首先,我们可以从表sc_users选择所有项目,然后找到sc_users和带有ID_USER的子查询,该子查询在注释中提到。

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

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

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