[英]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_USER
从sc_users_teaching
并找到ID_USER
从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. 因此,首先,我们可以从表
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.