TABLES :
cart
,order
,customer
ID :
id_cart
,date_add
,id_customer
FROM cart
firstname
,lastname
FROM customer
我需要获取数据:
Firstname
表格上不存在 Lastname
,Date_add
,id_Cart
,id_Cart
,但是ORDER
。
因此如果id_cart
表中CART
表中的ORDER
不存在,我会获得数据:id_Cart
,date_add
,id_customer
$sql = '
SELECT cu.firstname, cu.lastname, ca.date_add, ca.id_cart
FROM ps_cart ca
JOIN ps_customer cu
ON ca.id_customer = cu.id_customer
WHERE NOT EXISTS (
SELECT 1
FROM `ps_orders` o
WHERE o.id_cart = ca.id_cart
)';
$retval = mysql_query( $sql, $handle );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_NUM))
{
echo
"Name :{$row['firstname']} ".
"Lastname : {$row['lastname']} ".
"Date_add : {$row['date_add']} ".
"CART_ID : {$row['id_cart']} ".
"--------------------------------<br>";
}
mysql_free_result($retval);
echo "Fetched data successfully\n";
mysql_close($handle);
这是我的代码。看来你的查询是我需要的,但我得到了这样的结果:名称:姓氏:Date_add:CART_ID和很多行,但数据在哪里?没有错误..
答案 0 :(得分:1)
我会在黑暗中狂奔:
SELECT cu.firstname, cu.lastname, ca.date_add, ca.id_cart
FROM ps_cart ca
JOIN ps_customer cu
ON ca.id_customer = cu.id_customer
WHERE NOT EXISTS (
SELECT 1
FROM ps_orders o
WHERE o.id_cart = ca.id_cart
)';
将MYSQL_NUM
更改为MYSQL_ASSOC
,以便能够使用$row['column_name']