在两列中获取具有相同值的行数据

时间:2017-08-08 09:16:45

标签: php mysql

我试过这个: 这段代码:

$sql="SELECT DISTINCT t_no,cust_name
FROM menu_order
WHERE b_id IN (
    SELECT b_id,m_name
    FROM menu_order
    GROUP BY b_id
    HAVING COUNT(*) > 1
)";


$result = mysql_query($sql);
$row =  mysql_fetch_array($result);

     echo $row['t_no'];
     echo $row['cust_name'];
while ($row = mysql_fetch_assoc($result, MYSQL_ASSOC)) {

    $name = $row['m_name'];
    $n = explode(',', $name);
    foreach ($n as $d) {
        if ($d !== '') { // because before first comma or after last can be empty
            echo $d.",".PHP_EOL;

        }

    }

}

任何人都可以告诉我如何在php中使用t_no(table number)获取数据。 在这里,b_id(booking_id) is foreign key

预期输出

1    |T03  | 2017-08-07 | Dal Fry,Sahi Paneer,Aloo Paratha | 2,1,1
  

这是数据库:

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以将GROUP BYGROUP_CONCAT一起使用,例如:

SELECT b_id, t_no, date, GROUP_CONCAT(m_name), GROUP_CONCAT(quantity)
FROM table
GROUP BY b_id, t_no, date;

Here's MySQL GROUP_CONCAT()函数的文档。

答案 1 :(得分:0)

我认为您的查询存在问题..您的子查询将返回两列,包括名称,但b_id IN()只需要b_id。从子查询中删除m_name