簡體   English   中英

Select from table where 子句 from array from another table

[英]Select from table where clause from array from another table

我試圖在代碼中插入一個故障保護,以防止他們更進一步,我已經撓了一段時間了。 我能夠將某些內容放入數組中,但我無法從數組中獲取項目以匹配第二個選擇查詢。 我只從項目中獲取 Array 而不是值。

我的第一個選擇查詢是這樣的:

$datohenter3 = "select DATE_FORMAT(datotid, '%Y.%m.%d') AS dato from gramorapport34 group by dato order by dato asc";
$hentdatoer = $db->query($datohenter3);

$periodedatoer = array();
for ($x = 1; $x <= $db->affected_rows; $x++) {
    $periodedatoer[] = $hentdatoer->fetch_assoc();
}

然后我想將此數組中的值與我的下一個選擇查詢相匹配:

$rapportdatoer = "select fradato, tildato from gramorapportlogg WHERE  fradato IN('".$periodedatoer."') OR tildato IN('".$periodedatoer."')";
$rapportdatoeksist = $db->query($rapportdatoer);
if ( !$rapporteksist ) die('Database Error: '.$db->error);
    while($row = mysqli_fetch_array($rapportdatoeksist))
     {
        print_r($row);
     } 

我得到的錯誤是:

注意:第二次選擇的數組到字符串的轉換

數據庫錯誤:您的 SQL 語法有錯誤; 檢查與您的 MariaDB 服務器版本相對應的手冊,了解在第 1 行的“fradato IN('Array') OR tildato IN('Array')'附近使用的正確語法

我不是 JOIN SELECT 查詢的專家。 這是使用帶有 PHP7.2 的 MariaDB 10.3.12

var_dump 可在: https : //www.lokalradio.no/rapport/gramo/datohenttest.php

請注意, $periodedatoer是一個數組數組。 里面的每個元素都有 1 個dato鍵(如var_dump顯示的那樣)。

所以使用array-column來獲取值,然后內implode為:

$rapportdato = implode("','", array_column($periodedatoer, "dato"));

現在您可以在第二個查詢中使用$rapportdato作為:

$rapportdatoer = "select fradato, tildato from gramorapportlogg WHERE  fradato IN('" . $rapportdato . "') OR tildato IN('" . $rapportdato . "')";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM