mysql in()使用语法并修复sql查询

时间:2013-06-16 10:17:18

标签: php mysql sql

我需要一个小修复查询请帮助我,在不修复。请参阅底部,我的示例代码,thx for all helpers ..

ny_xd_satilanlar database rows
id---uyid---toplam---sipno
01---16---1---1237777777
02---30---3---1301275922
03---30---2---1301741801
04---11---4---1123467988


//NOT WORK RETURN : 0 (zero)
$total_urun = mysql_fetch_array(mysql_query("SELECT SUM(adet) as toplam FROM ny_xd_satilanlar WHERE sipno IN ('{$curTel['sipnolar']}') AND uid='{$urunid}'"));
$total_urun = $total_urun[toplam];

// EXAMPLE 1 = NOT WORK RETURN : 0 (zero)  HOW TO FİX QUERY ????
SELECT SUM(adet) as toplam FROM ny_xd_satilanlar WHERE sipno IN ('1237777777,1301275922,1301741801') AND uid='30'

// EXAMPLE 2 = NOT WORKİNG TRUE RETURN : 3 (three)  HOW TO FİX QUERY ????
SELECT SUM(adet) as toplam FROM ny_xd_satilanlar WHERE sipno IN ('1301275922,1301741801') AND uid='30'

// EXAMPLE 1 ALTERNATİVE QUERY = OK WORK RETURN 3+2= :5 (five) 
SELECT SUM(adet) as toplam FROM ny_xd_satilanlar WHERE (sipno='1237777777' OR sipno='1301275922' OR sipno='1301741801') AND uid='30'

需要一个FİX第二个查询?

2 个答案:

答案 0 :(得分:1)

您需要使用单引号字符分隔您希望IN子句使用的值:

SELECT SUM(adet) as toplam FROM ny_xd_satilanlar WHERE sipno IN ('1237777777','1301275922','1301741801') AND uid='30';

SELECT SUM(adet) as toplam FROM ny_xd_satilanlar WHERE sipno IN ('1301275922','1301741801') AND uid='30';

答案 1 :(得分:1)

您需要分隔IN值。目前,所有值都包含在'中,因此它们表现为单个值。除非正确转义,否则任何包含在引号中的内容都将被视为一个值。

SELECT SUM(adet) as toplam FROM ny_xd_satilanlar WHERE sipno IN (1237777777, 1301275922, 1301741801) AND uid='30'

SELECT SUM(adet) as toplam FROM ny_xd_satilanlar WHERE sipno IN (1301275922, 1301741801) AND uid='30'

或者,如果它们应该是字符串数字:

SELECT SUM(adet) as toplam FROM ny_xd_satilanlar WHERE sipno IN ('1237777777', '1301275922', '1301741801') AND uid='30'

SELECT SUM(adet) as toplam FROM ny_xd_satilanlar WHERE sipno IN ('1301275922', '1301741801') AND uid='30'