【问题标题】:How to get specific data from table1, use it in table2如何从table1中获取特定数据,在table2中使用
【发布时间】:2019-06-24 08:58:19
【问题描述】:

我正在尝试获取表 1 (stellingen) 中的特定行。我想存储这些行以指定我对第二个表(stelling)感兴趣的行。因此,假设表 1 有 5 行,其中 stelling ID 与 REGIOID = 5 匹配。这些来自 stelling ID 的 IDS 我想用来从第二个表中获取数据。查看代码以查看我尝试过的内容。我也没有设法找到一种方法来实现这一点。

所以也许太清楚了,因为人们总是说我不清楚: 有两张桌子。他们都有一个匹配的列。我试图告诉第二个表我想要数据,但前提是它与第一个表的数据匹配。就像一棵树的树枝。然后,我想输出第二个表中的一些数据。

我以前尝试过这样的事情:

SELECT 
  * 
FROM 
  table2 
LEFT JOIN 
  table1 ON 
    table1.ID = table2.table1_id 

我尝试创建一个 while 循环来获取之前的数据(在第一个 if 语句之后,最后一个 += 用于变量 $amountofstellinge):

    $amountOfStellinge = 0;
    while ($amountOfStellinge<5){
        mysqli_data_seek($result, $amountOfStellinge);

这是代码现在的样子,它是错误的,我一直在搞砸,但也许它向您展示了我想要更好地实现的目标。

if($result = mysqli_query($con, "SELECT * FROM stellingen WHERE REGIOID=1;")) {

        $row = mysqli_fetch_assoc($result);

        $stellingid= $row["Stelling_ID"];
        //checking.. and the output is obviously not what I want in my next query
        printf($stellingid);

    //defining the variable
        $Timer=0;

$sql1="SELECT * FROM stelling WHERE stelling_iD=$stellingid ORDER BY Timer DESC;";
$records2 =  mysqli_query($con, $sql1);
$recordscheck = mysqli_num_rows($records2);
//max 5 data
if ($recordscheck < 5){
    while ($stelling = mysqli_fetch_assoc($records2)){
        //At the end, i would like to only have the data that is most recent
        $Timer = date('d F', strtotime($stelling['Timer']));



        echo "<p><div style='color:#ED0887'>".$Timer.":</div><a target = '_blank' style='text-decoration:none' href='".$stelling['Source']."'>".$stelling['Title']."</a></p>";

    }}
        $recordscheck+=1;   } // this is totally wrong

编辑: 我试过了,@noobjs

$Timer=0;

$sql1="SELECT 
* 
FROM 
stelling 
LEFT JOIN 
stellingen 
ON 
stelling.ID = stellingen.stelling_id 
WHERE 
stellingen.REGIOID=1
ORDER BY stelling.Timer  LIMIT 5 DESC ;";

$records2 =  mysqli_query($con, $sql1);
printf($records2);
    while ($stelling = mysqli_fetch_assoc($records2)){
        $Timer = date('d F', strtotime($stelling['Timer']));



        echo "<p><div style='color:#ED0887'>".$Timer.":</div><a target = '_blank' style='text-decoration:none' href='".$stelling['Source']."'>".$stelling['Title']."</a></p>";
       }

出现此错误:

警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,布尔值在

中给出

编辑以获得更多说明 这是一些示例数据

预期结果是: 每个页面都使用来自不同 REGIOID 的数据。我希望该页面显示来自表 stelling(表 1)的数据。根据REGIOID(表2)

【问题讨论】:

  • 只能用mysql解决。编辑您的问题,显示示例数据、表格结构和预期结果。
  • @LelioFaieta 谢谢 Lelio,我忘了写预期结果。
  • 使用文字而不是图片。顺便说一句,这样的图片是不可能阅读的

标签: php database mysqli while-loop


【解决方案1】:

如果我理解正确:

SELECT 
  * 
FROM 
  stelling 
LEFT JOIN 
  stellingen 
ON 
  stelling.stellingID = stellingen.stelling_id 
WHERE 
  stellingen.REGIOID=1
ORDER BY stelling.Timer  DESC LIMIT 5 ;

【讨论】:

  • 看起来很有趣,我要去试试。
  • 除了 myquestion @noobjs 之外,我还要把我尝试过的东西放上去
  • mysql 在 DESC 位置抛出错误。我现在编辑我没有得到 mysql 的任何错误。
猜你喜欢
  • 2020-08-14
  • 2014-10-02
  • 1970-01-01
  • 1970-01-01
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
相关资源
最近更新 更多