2014-07-18 4 views
0
INSERT INTO `stock` (`stock_id`, `product_attributes`, `product_id`, `qty`) VALUES 
(43, '9,11', 2, 0), 
(43, '9,12', 2, 10), 
(44, '9,13', 2, 20), 
(45, '10,11', 2, 0), 
(46, '10,12', 2, 30), 
(47, '10,13', 2, 50), 
(48, '14,11', 2, 0), 
(49, '14,12', 2, 0), 
(50, '14,13', 2, 0); 

은이 같은 데이터 테이블을 가지며, 제품 컬럼 데이터는 문자열 형태 예에 (9,11)디스플레이 MySQL의 테이블 데이터

I 모두에 양 제로 한 제품을 나열 할 발생.

SELECT 
    product_attributes, 
    substring_index(product_attributes,',',-1) as one, 
    substring_index(product_attributes,',',1)as two 
FROM 
    (SELECT * 
    FROM stock 
    ORDER BY substring_index(product_attributes,',',1) ASC, 
       substring_index(product_attributes,',',-1) ASC) AS ordered 
WHERE 
    substring_index(product_attributes,',',1) NOT IN (
    SELECT substring_index(product_attributes,',',1)as one FROM stock WHERE qty!='0') 
GROUP BY one' 

데이터베이스에서이 쿼리를 실행하여 올바른 결과를 얻었습니다!

http://i.stack.imgur.com/Gm9yp.jpg

가 지금은

mysql_select_db('fly_stock'); 

// run query 
$q = mysql_query('select product_attributes, substring_index(product_attributes,',',-1)as one,substring_index(product_attributes,',',1)as two from(SELECT * FROM stock order by substring_index(product_attributes,',',1)ASC, substring_index(product_attributes,',',-1)asc)as ordered where substring_index(product_attributes,',',1)not in (select substring_index(product_attributes,',',1)as one from stock where qty!='0')group by one'); 

//print the items 
while($row = mysql_fetch_array($q)) { 
    echo $row['one'] . " " . $row['two']; 
    echo "<br>"; 
} 
mysql_close($con); 
?> 

날 PHP 코드를 편집 할 수 있도록 도와 열 하나, (쉼표) 열이 ... 즉, (14,11)을 표시 할 14 있도록, 11은 PHP 페이지에 표시됩니다.

+4

mysql_query는 쓸모없는 인터페이스이므로 새로운 응용 프로그램에서 사용해서는 안되고 이후 버전의 PHP에서 제거 될 것입니다. [PDO와 같은 현대적인 대체물은 배우기 어렵지 않습니다.] (http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). PHP를 처음 사용하는 분이라면 [PHP The Right Way] (http://www.phptherightway.com/)와 같은 안내서가 모범 사례를 설명하는 데 도움이 될 것입니다. – tadman

+0

원하는 경우이 간단한 두 단계 과정을 따르십시오. 1. 아직 수행하지 않았다면 문제를보다 쉽게 ​​복제 할 수 있도록 적절한 DDL (및/또는 sqlfiddle)을 제공하십시오. 2. 아직 수행하지 않은 경우 1 단계에서 제공된 정보에 해당하는 원하는 결과 세트를 제공하십시오. – Strawberry

+0

작은 따옴표를 사용하여 문제가 발생했습니다! thanks @jage – user3004860

답변

1

당신은 당신이 당신의 PHP를 기반으로 보조 노트에 mysql_fetch_assoc

//print the items 
while($row = mysql_fetch_assoc($q)) { 
    echo $row['one'] . " " . $row['two']; 
    echo "<br>"; 
} 

, 할 때 mysql_fetch_array을 사용하고, 사용자는 mysql_query() 함수는 쿼리가 하나의 문자열에 있기 때문에 가능성이 오류를 생산하고 또한 따옴표로 묶지 않고 SQL에서 작은 따옴표를 사용하여 이스케이프 처리하지 않습니다.

+1

mysql_fetch_array의 기본값은 연관 배열과 숫자 배열을 제공하고 있습니다. 그래서 그게 문제가 아니에요. 하지만 당신은 이미 인용문을 언급했습니다 ... – hellcode

0

여기에 뭔가 빠져 있거나 정말로 PHP에 one,two 출력을 원하십니까?

while($row = mysql_fetch_assoc($q)) { 
    echo implode("," array($row['one'], $row['two'])); 
    echo "<br>"; 
}