简体   繁体   English

从两个MySQL表创建一个过滤的数组

[英]Create a filtered array from two MySQL table

Here's the situation. 这是情况。

I have two table in a DB: tblcustomfieldsvalues (for custom fields entry) and tblclients (contains client list). 我在DB中有两个表: tblcustomfieldsvalues (用于自定义字段条目)和tblclients (包含客户端列表)。

tblcustomfieldsvalues has following data structure: tblcustomfieldsvalues具有以下数据结构:

id => 10 relid => 13 data => somedataentry id => 10 relid => 13 data => somedataentry

id => 10 relid => 21 data => someotherdataentry id => 10 relid => 21 data => someotherdataentry

tblclients has following data structure: tblclients具有以下数据结构:

id => 13 firstname => somename lastname => somelastname id => 13 firstname => somename lastname => somelastname

I have this code to create an array of relids which have id = 10: 我有这个代码来创建一个id = 10的relids数组:

$sql = mysqli_fetch_array(mysqli_query("SELECT * FROM `tblcustomfieldsvalues` WHERE `id` = '10'"));

$cids = array();

while ($row = $sql)
{
    array_push($cids, $row['relid']);
}

Now that I have user IDs of those who have filled their custom fields with some data in the $cids array, how can I get those users details from tblclients ? 既然我拥有那些已经在$cids数组中填充了自定义字段并包含一些数据的用户ID,那么如何从tblclients获取这些用户的详细信息?

Thanks in advance. 提前致谢。

$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
    }
}

OR 要么

 SELECT * FROM
 tblcustomfieldsvalues cv,tblclients c WHERE
 cv.id = 10 and cv.id = c.id

Sounds like you just need to use an INNER JOIN : 听起来你只需要使用INNER JOIN

SELECT t2.*
FROM tblcustomfieldsvalues t
  INNER JOIN tblclients t2 ON t.relid = t2.id 
WHERE t.ID = 10

Good luck! 祝好运!

Without knowing the table structure of tblclients I can't be sure, but it sounds like instead of doing separate queries and looping you could just use a join: 在不知道tblclients的表结构的tblclients我无法确定,但听起来不是单独进行查询和循环,而是可以使用连接:

SELECT
    *
FROM
    tblcustomfieldsvalues
    NATURAL JOIN tblclients
WHERE
    id = 10

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

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