问题
Here's the MySQL:
SELECT *
FROM tbl1
WHERE name > (
SELECT name
FROM tbl1
WHERE id = 3
)
ORDER BY name
Here's my best guess at the Propel code but it's not working:
$c = new Criteria();
$c->addAscendingOrderByColumn(CartPeer::ITEM_NAME);
$c->add(CartPeer::CATEGORY, $c->add(CartPeer::ITEM_ID, $item->getItemId()), Criteria::GREATER_THAN);
In respond to this question
回答1:
Propel doesn't have a standard way of doing subqueries as part of a criteria.
You can either separate your query (obtain the value you want to compare against first and then use it into the original query) or use a CUSTOM
criteria with your subquery in your propel query.
Here's an example of the second option:
$c = new Criteria();
$subSelect = "cart.category > (
SELECT cart.category
FROM carts
WHERE carts.id = 3)";
$c->add(CartPeer::CATEGORY, $subSelect, Criteria::CUSTOM);
EDIT: Here's an example of the first option
// find the object we want to compare against
$c = new Criteria();
$c->add(CartPeer::ID, 3);
$cart = CartPeer::doSelectOne($c)
// then make the actual criteria
$c = new Criteria();
$c->add(CartPeer::CATEGORY, $cart->getCategory(), Criteria::GREATER_THAN)
The only problem with this option is that you are making two queries instead of one, which may hit on your performance, but it depends on your application of course.
来源:https://stackoverflow.com/questions/15148913/how-to-convert-this-mysql-statement-to-symfony-propel