mysql如果存在变量

时间:2014-07-16 10:34:07

标签: mysql variables

TABLES

cartordercustomer

ID

id_cartdate_addid_customer FROM cart

firstnamelastname FROM customer

我需要获取数据

Firstname表格上不存在

LastnameDate_addid_Cartid_Cart,但是ORDER

因此如果id_cart表中CART表中的ORDER不存在,我会获得数据:id_Cartdate_addid_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和很多行,但数据在哪里?没有错误..

1 个答案:

答案 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']

提取数据