How to create a sql query with multiple conditions?

Hi everyone!

I am trying to create a sql query with multiples conditions, but despite I have readen all the the information about it I am not able to allow my code works.

I have the following sql code:




$query=Yii::app()->db->createCommand()

->select('*')

->from('articulos')

->where (['and', 'premium=Premium', 'estadoNoticia=Publicada' ])

->order('fecha DESC')

->limit(3)

->queryAll();



The problem of this code is that despite I have a column called premium in my sql database, and another one called estadoNoticia, Yii fwk throws the following error:

I attach an image with my table structure.

Finally, what if I want to do an or condition plus and and condition? I mean, how I have to writte in yii the sql statement to throw the next query?




Select * 

from articulos 

where (estadoNoticia=Publicada) AND (seccion=Nacional) AND (premium=Premium) or (premium=1) 

order by fecha desc 

limit 3;



Thank you very much!!

Try this


<?php

$user=Yii::app()->db->createCommand()

->select("*")

->from("articulos")

->andWhere("estadoNoticia=:estadoNoticia",array(':estadoNoticia'=>'Publicada'))

->andWhere("seccion=:seccion",array(':seccion'=>'Nacional'))

->order(fecha)   

->queryAll();

print_r($user);

?>

Thank you Aneesh! It works fine! :rolleyes: