情况就是这样。
我在数据库中有两个表:tblcustomfieldsvalues
(用于自定义字段条目)和tblclients
(包含客户端列表)。
tblcustomfieldsvalues
具有以下数据结构:
id
=> 10
relid
=> 13
data
=> somedataentry
id
=> 10
relid
=> 21
data
=> someotherdataentry
tblclients
具有以下数据结构:
id
=> 13
firstname
=> somename
lastname
=> somelastname
我有这段代码来创建一个relids
的数组id
= 10:
$sql = mysqli_fetch_array(mysqli_query("SELECT * FROM `tblcustomfieldsvalues` WHERE `id` = '10'"));
$cids = array();
while ($row = $sql)
{
array_push($cids, $row['relid']);
}
现在,我拥有那些使用$cids
数组中的某些数据填充自定义字段的用户ID,如何从tblclients
获取这些用户的详细信息?
提前致谢。
答案 0 :(得分:1)
$sql = mysqli_fetch_array(mysqli_query("SELECT * FROM `tblcustomfieldsvalues` WHERE `id` = '10'"));
$cids = array();
while ($row = $sql)
{
//array_push($cids, $row['relid']);
$sql1 = mysqli_fetch_array(mysqli_query("SELECT * FROM `tblclients ` WHERE `id` = '$row['relid']'"));
while ($row1 = $sql1)
{
//echo your output
}
}
或强>
SELECT * FROM
tblcustomfieldsvalues cv,tblclients c WHERE
cv.id = 10 and cv.id = c.id
答案 1 :(得分:1)
听起来你只需要使用INNER JOIN
:
SELECT t2.*
FROM tblcustomfieldsvalues t
INNER JOIN tblclients t2 ON t.relid = t2.id
WHERE t.ID = 10
祝你好运!
答案 2 :(得分:0)
在不知道tblclients
的表结构的情况下我无法确定,但听起来不是单独进行查询和循环,而是可以使用连接:
SELECT
*
FROM
tblcustomfieldsvalues
NATURAL JOIN tblclients
WHERE
id = 10