php - 从同一个表中获取多个值(乱序的)

标签 php mysql

我想从单个表中获取值行。我想获取特定 ID 的 sub_id。

我在 2 个查询中实现了我的要求。我想在单个查询中完成。我想显示结果 'Event,order history,Event Ticket,calander'。

enter image description here

$sql="select * from table1 where roles like %admin% and sub_id='0'"
$sql1=mysql_query($sql);
while($fet=mysql_fetch_assoc($sql1))
{
  $id=$fet['id'];
  $query="select page_name from table1 where sub_id= '$id'";
  .. ..
}

最佳答案

你需要的是subqueries .

基本上,您希望选择一些与另一个查询结果给出的条件相匹配的行。

下面的示例应该可以解决您的问题并阐明概念:

select
  page_name
from
  table1
where
  sub_id in (
    select 
      id
    from
      table1
    where
      roles like '%admin%'
      and sub_id = 0
  )

关于php - 从同一个表中获取多个值(乱序的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35194714/

相关文章:

php - XML 解析子问题

PHP Yii 标准选择有限制的随机记录

java - Hessian:Java 客户端无法连接到 php 服务器

php - 如何创建字符串的排列(10 个字符或更多)?

mysql - 使用UPDATE函数批量更新记录

javascript - ajax $_POST 数组丢失

php - 为什么要编写 MySQL 查询 "programmatically like active record?"

php - 代码在 php 中创建多维数组而不是二维数组,这是怎么回事?

mysql - 如何替换 View 查询中的 UNION?

mysql - 让重复 key mysql正常工作