简体   繁体   English

在MySQL中使用数组查询

[英]Query With array in mysql

I want to fetch contents of two separate category and insert them in two different boxes. 我想获取两个单独类别的内容,然后将其插入两个不同的框中。 I have written the query like this: 我写了这样的查询:

$query = mysql_query(sprintf("SELECT * FROM ".DB_PREFIX."_links WHERE cat = %d LIMIT %d" , 4, 3));

But can we write 2 or more query in one query? 但是我们可以在一个查询中写两个或两个以上查询吗? like :"select * from blah where cat = (3, 4, 5)" 例如:“从*中选择*,其中cat =(3,4,5)”

EDIT: 编辑: 替代文字

Thanks in advance 提前致谢

Update 2 更新2

To load your data in a specific div, you can store the result in an array and later echo it where you want like this: 要将数据加载到特定的div中,可以将结果存储在数组中,然后在需要的地方回显它,如下所示:

$result = mysql_query(...........);

$i = 0;
$data = array();

while($row = mysql_fetch_array($result)){
    $data[$i] = $row;
    $i++;
}

HTML: HTML:

<!-- For div 1 -->
<div>
  <?php echo $data[0]['fieldName']?>
</div>

<!-- For div 2 -->
<div>
  <?php echo $data[1]['fieldName']?>
</div>

<!-- For div 3 -->
<div>
  <?php echo $data[2]['fieldName']?>
</div>

Update 更新资料

You can do something like this:

    $result = mysql_query(...........);
    while($row = mysql_fetch_array($result)){
      echo '<div>' . $row['fieldName'] . '</div>'
    }

This way each record will appear in different div. 这样,每个记录将出现在不同的div中。


But can we write 2 or more query in one query? 但是我们可以在一个查询中写两个或两个以上查询吗? like :"select * from blah where cat = (3, 4, 5)" 例如:“从*中选择*,其中cat =(3,4,5)”

You can do so with IN operator: 您可以使用IN运算符:

select * from blah where cat IN (3, 4, 5)

The IN operator allows you to specify multiple values in a WHERE clause. IN运算符允许您在WHERE子句中指定多个值。

More Information: 更多信息:

只需使用SQL的IN语句

SELECT * FROM _links WHERE cat IN (3,4,5)

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

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