简体   繁体   English

MySQL查询到数组到MySQL IN查询

[英]MySql Query to Array to MySql IN Query

I am attempting to make an array of IDs and use that array in another query to make sure the 2nd query results have the same ids that are in the array. 我试图制作一个ID数组,并在另一个查询中使用该数组,以确保第二个查询结果具有与数组中相同的ID。

As per the requirements of the rest of my code, I would prefer not to use the join method. 根据我其余代码的要求,我宁愿不使用join方法。

    $topartistsquery = "SELECT main_id FROM pages WHERE catnum = '201' AND top100 = 'y'";
    $result = mysql_query($topartistsquery);
    $topartistarray = array();
    while(($row =  mysql_fetch_assoc($result))) {
        $topartistarray[] = $row['main_id'];
    }
    //print_r($topartistarray);

    $eventquery = mysql_query("SELECT * FROM TNDB_CSV2 WHERE City = '".$city."' AND PCatID = '2' AND PerformerID IN ($topartistarray) AND DateTime >= CURDATE() ORDER BY DateTime LIMIT 3");

Here is the output of the $topartistarray: 这是$ topartistarray的输出:

Array ( [0] => 28 [1] => 1126 [2] => 47643 [3] => 2769 [4] => 41512 [5] => 1429 [6] => 163 [7] => 48418 [8] => 198 [9] => 3748 [10] => 10161 [11] => 39890 [12] => 14160 [13] => 25407 [14] => 29219 [15] => 36980 [16] => 1860 [17] => 41299 [18] => 342 [19] => 7468 [20] => 33205 [21] => 1564 [22] => 33911 [23] => 15183 [24] => 540 [25] => 974 [26] => 8358 [27] => 30678 [28] => 4804 [29] => 4266 [30] => 517 [31] => 522 [32] => 1550 [33] => 15989 [34] => 930 [35] => 3383 [36] => 26468 [37] => 5560 [38] => 1063 [39] => 2870 [40] => 4055 [41] => 24917 [42] => 46223 [43] => 973 [44] => 334 [45] => 27970 [46] => 27985 [47] => 356 [48] => 6655 [49] => 201 [50] => 1930 [51] => 1069 [52] => 38053 [53] => 397 [54] => 21713 [55] => 53725 [56] => 4941 [57] => 864 [58] => 41904 [59] => 59233 [60] => 53970 [61] => 50875 [62] => 54175 [63] => 56861 [64] => 53317 [65] => 55258 [66] => 823 [67] => 55704 [68] => 59797 [69] => 27866 [70] => 123 [71] => 59749 [72] => 1567 [73] => 34851 [74] => 3399 [75] => 58674 [76] => 59442 [77] => 60334 ) 

Any constructive input is greatly appreciated. 非常感谢任何建设性的投入。 Thank you in advance 先感谢您

Try this: 尝试这个:

if (isset($topartistarray) and count($topartistarray) {
    $eventquery = mysql_query("SELECT * FROM TNDB_CSV2 WHERE City = '".$city."' AND PCatID = '2' AND PerformerID IN (".implode(",",$topartistarray).") AND DateTime >= CURDATE() ORDER BY DateTime LIMIT 3");
} else {
    $eventquery = false;
}

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

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