rajwa766
rajwa766

Reputation: 644

How to get the first row of table in yii2 with where condition?

I need to get the first row of table with where condition.

(new Query())
        ->select('*')
        ->from('stock_in')
        ->where("user_id = '$request_user_id'")
        ->andWhere("product_id = '$product_id'")
        ->andWhere("remaining_quantity > '0'")
        ->one();

Upvotes: 3

Views: 3781

Answers (2)

rajwa766
rajwa766

Reputation: 644

Yes i got my answer.Use Limit there

(new Query())
        ->select('*')
        ->from('stock_in')
        ->where("user_id = '$request_user_id'")
        ->andWhere("product_id = '$product_id'")
        ->andWhere("remaining_quantity > '0'")
        ->limit('1')
        ->one();

Upvotes: 4

Marcos
Marcos

Reputation: 1370

To limit the result of a query you should use limit().

(new Query())
    ->select('*')
    ->from('stock_in')
    ->where("user_id = '$request_user_id'")
    ->andWhere("product_id = '$product_id'")
    ->andWhere("remaining_quantity > '0'")
    ->limit(2)
    ->all();

Documentation: http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html#limit-offset

Upvotes: 0

Related Questions