如何获取记录表格表具有不同的列值和相同的外键?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取记录表格表具有不同的列值和相同的外键?相关的知识,希望对你有一定的参考价值。
我有三个名为的表:
orders
,orderitems
orderitemattributes
我需要得到orderitemattribute_name = "OPTION_DATE"
和orderitemattribute_value = "01-09-2018"
和orderitemattribute_name = "OPTION_SIZE"
的记录;
单orderitems
有许多orderitemattributes
记录,如:OPTION_DATE
,OPTION_SIZE
$rentaldateconstant="OPTION_DATE";
$todaydate="01-09-2018";
$shoesize="OPTION_SIZE";
$query = JFactory::getDbo()->getQuery(true);
$query->select('oi.orderitem_id,oi.orderitem_name,oi.product_id,oi.orderitem_quantity');
$query->select('oi.order_id');
$query->select('oia.orderitemattribute_name,oia.orderitemattribute_value');
$query->from('#__orders AS jo');
$query->leftJoin('#__orderitems AS oi ON oi.order_id=jo.order_id');
$query->leftJoin('#__orderitemattributes AS oia ON oia.orderitem_id=oi.orderitem_id');
$query->where('oia.orderitemattribute_name = '.$db->Quote($rentaldateconstant).' AND oia.orderitemattribute_value ='.$db->Quote($todaydate));
$query->orwhere('oia.orderitemattribute_name = '.$db->Quote($shoesize));
您似乎没有存储连接对象(JFactory::getDbo()
)。我将包含一个完整的try-catch
块来向您展示一些最佳实践编码,这将在将来需要调试时为您节省大量时间。
如果你编写代码来捕获错误,你会得到:
致命错误:在null上调用成员函数Quote()
换句话说,$db
没有定义,是null
。这意味着quote()
没有资源来运作。
建议/未经测试的代码:
$rentaldateconstant="OPTION_DATE";
$todaydate="01-09-2018";
$shoesize="OPTION_SIZE";
$db = JFactory::getDbo(); // you should call this once and use the variable when needed
try {
$query = $db->getQuery(true)
->select(
array(
"oi.orderitem_id",
"oi.orderitem_name",
"oi.product_id",
"oi.orderitem_quantity",
"oi.order_id",
"oia.orderitemattribute_name",
"oia.orderitemattribute_value"
)
)
->from("#__orders AS jo")
->leftJoin("#__orderitems AS oi ON oi.order_id=jo.order_id"
->leftJoin("#__orderitemattributes AS oia ON oia.orderitem_id=oi.orderitem_id"
->where("oia.orderitemattribute_name = " . $db->q($rentaldateconstant) . " AND oia.orderitemattribute_value = " . $db->q($todaydate))
->orwhere("oia.orderitemattribute_name = " . $db->q($shoesize));
echo $query->dump(); // to see what you've generated (never show to public)
$db->setQuery($query);
var_export($db->loadObjectList()); // to see what your resultset looks like as an array of objects
} catch (Exception $e) {
echo "Syntax Error " , $e->getMessage(); // never show exact error details to public
}
我还想质疑你的WHERE
子句逻辑。我不知道您的项目逻辑需要什么,但这是您查询中生成的子句:
(oia.orderitemattribute_name ='OPTION_DATE'和oia.orderitemattribute_value = '01 -09-2018')或(oia.orderitemattribute_name ='OPTION_SIZE')
但是,我认为你的目标是将条件与AND
结合在一起。
->where(
array(
"oia.orderitemattribute_name = " . $db->q($rentaldateconstant),
"oia.orderitemattribute_value = " . $db->q($todaydate),
"oia.orderitemattribute_name = " . $db->q($shoesize)
)
)
这会产生:
WHERE oia.orderitemattribute_name = 'OPTION_DATE'
AND oia.orderitemattribute_value = '01-09-2018'
AND oia.orderitemattribute_name = 'OPTION_SIZE'
最后,一小段建议......将日期值保存到数据库时,我建议使用DATE类型列并将值存储为YYYY-MM-DD
,以便将来可以更轻松地查询/操作数据。
以上是关于如何获取记录表格表具有不同的列值和相同的外键?的主要内容,如果未能解决你的问题,请参考以下文章