2012-11-17 48 views
3

我正在嘗試使系統自動打印訂單,而且我現在正面臨一個問題。 我想在打印機瞭解的格式中創建一個字符串,現在,我需要在發送之前從數據庫收集數據。現在我的問題是,在某些情況下,order_id是相同的。這裏有一個例子:在PHP中顯示具有相同ID的行的值

+----------+----------------------+ 
| order_id |  product_name  | 
+----------+----------------------+ 
|  1 | Pizza with cheese | 
+----------+----------------------+ 
|  1 | Coca-Cola Zero | 
+----------+----------------------+ 
|  2 |  Spaghetti  | 
+----------+----------------------+ 
|  3 |  Lasagna  | 
+----------+----------------------+ 

那麼,我試圖在收集所有「PRODUCT_NAME」的其中具有相同的‘ORDER_ID’一個字符串,因此它顯示爲:奶酪 「比薩,可口可樂零「

有沒有辦法做到這一點?

在此先感謝。

編輯:我不知道我是否清楚地提到它,但我想製作一個PHP腳本來顯示它。對不起,如果我很困惑。

+1

「有沒有辦法做到這一點?」 =>絕對。我做了一次。你可以使用'GROUP_CONCAT'。您可以手動連接結果。在PHP中可能有一些很好的函數式方法,它可以讓你在不用編寫單個循環的情況下在PHP中執行它。 –

回答

2

你可以使用

SELECT order_id, GROUP_CONCAT(product_name SEPARATOR ', ') AS product_names FROM orders GROUP BY order_id; 

例在PHP中:

<?php 
    $db_server = 'localhost'; 
    $db_user = 'user'; 
    $db_pass = 'pass'; 
    $db_name = 'database'; 

    $con = mysql_connect($db_server, $db_user, $db_pass) 
     or die('Could not connect to the server!'); 

    mysql_select_db($db_name) 
     or die('Could not select a database.'); 


    $sql = "SELECT order_id, GROUP_CONCAT(product_name SEPARATOR ', ')"; 
    $sql .= " AS product_names FROM orders GROUP BY order_id"; 

    $result = mysql_query($sql) 
     or die('A error occured: ' . mysql_error()); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> 
    <title>test</title> 
</head> 

<body> 
<h1>orders</h1> 
<table> 
    <tr><th>orders_id</th><th>product_names</th></tr> 
<?php 
while ($row = mysql_fetch_assoc($result)) { 
    printf("<tr><td>%d</td><td>%s</td></tr>\n", $row['order_id'], 
               $row['product_names']); 
} 
?> 
</table> 
</body> 
</html> 
+0

olafs答案涵蓋已經... –

+0

非常感謝。 – Ismail

+0

olafs答案涵蓋了group_concat,但它不是group_concat的一個很好的例子,因爲產品名稱列的值將爲'Pizza with cheeseCoca-Cola Zero'。這個答案是一個在group_concat方法中使用分隔符的例子。 – iRaS

4

可以爲此

select order_id, count(*), group_concat(product_name) 
from orders 
group by order_id 
having count(*) > 1; 

這個選擇給你所有order_id S的有條目的數量和連接到一起的訂單多個條目。如果您不需要該號碼,請在列列表中省略count(*)

如果你想所有訂單漏下having

select order_id, count(*), group_concat(product_name) 
from orders 
group by order_id 

並使用從PHP

$conn = new mysqli($host, $user, $pass, $db); 
// use the appropriate select 
$result = $conn->query('select order_id, group_concat(product_name) ' 
         . 'from orders group by order_id'); 
while ($row = $result->fetch_array()) { 
    // process the result row 
} 

這當然只是一個簡要介紹。您必須添加錯誤處理並填寫您想要對結果進行的操作。

+0

@JanDvorak謝謝你指出這一點,我在答案中反映了這一點。 –

+0

我可以在PHP中使用這個嗎? – Ismail

+0

@IsmailIsmaiil你是什麼意思?你當然可以調用'mysqli_query($ link,'select order_id,count(*),group_concat ...')並遍歷結果集。 –

1

您應該將您的數據拆分爲兩個單獨的表格,因爲存在相關的聯繫1:N其中一個訂單可以有多個產品。

CREATE TABLE `order` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    .... any further information about the order, 
    PRIMARY_KEY(`id`) 
); 

CREATE TABLE `order_product` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `order_id` int(11) DEFAULT NULL, 
    `product_name` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    CONSTRAINT `order_product_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `order` (`id`) ON DELETE CASCADE ON UPDATE CASCADE 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

這樣你沒有多orderIds一階

獲取一個訂單

select * from order where id=1 

獲取訂單的所有產品

select op.* from order o inner join order_product op on op.orderId=o.id 
+0

我不能分解它,因爲它是PrestaShop sql的一部分,如果我將它分開,我需要更改整個CMS – Ismail

+0

因此,您建議多個查詢?對不起,我沒有贊成。 –

+0

這是/應該是方法。問題中的設計打破了「關係」概念的目的。 – itachi

相關問題