简体   繁体   English

Select from table where 子句 from array from another table

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

I'm trying to insert a failsafe into a code to prevent them from going a step further, and I've been scratching my head for a while now.我试图在代码中插入一个故障保护,以防止他们更进一步,我已经挠了一段时间了。 I am able to put something into an array, but I am not able to get the items from the array to match the second select query.我能够将某些内容放入数组中,但我无法从数组中获取项目以匹配第二个选择查询。 I only get Array instead of the value from the item.我只从项目中获取 Array 而不是值。

My first select query is this:我的第一个选择查询是这样的:

$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();
}

Then I want to match the values from this array with my next select query:然后我想将此数组中的值与我的下一个选择查询相匹配:

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

The errors I am getting are:我得到的错误是:

Notice: Array to string conversion for the second select注意:第二次选择的数组到字符串的转换

Database Error: You have an error in your SQL syntax;数据库错误:您的 SQL 语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' fradato IN('Array') OR tildato IN('Array')' at line 1检查与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行的“fradato IN('Array') OR tildato IN('Array')'附近使用的正确语法

I'm not an expert in JOIN SELECT queries.我不是 JOIN SELECT 查询的专家。 This is using MariaDB 10.3.12 with PHP7.2这是使用带有 PHP7.2 的 MariaDB 10.3.12

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

Notice that $periodedatoer is an array of array.请注意, $periodedatoer是一个数组数组。 Each element inside has 1 key of dato (as you var_dump displays).里面的每个元素都有 1 个dato键(如var_dump显示的那样)。

So use array-column to get the values and then implode as:所以使用array-column来获取值,然后内implode为:

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

Now you can use $rapportdato in your second query as:现在您可以在第二个查询中使用$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