In almost in every wiki simple things are explained. I'm stuck in yii's CDbcriteria compare issue.
Only the exact "equal to" match is explained
for:
select * from users where status ='active'
This comparison is explained:
$criteria->compare('status','active');
But I can't find an example script which describes it with an operator based search. Like not equal to for the following query:
select * from users where status !='active'
How can I do this?
try some thing like this
$criteria->condition = " status<>'active'";
$criteria->compare('status',$this->status,true);
$criteria->addNotInCondition('status', array('active'));
if count(array('active')) === 1 sql will status != 'active', else status NOT IN ('active', 'smth else')
Try
$criteria->addCondition("NOT status = 'active'");
I had the below 3 conditions and it works like a charm:
$criteria->condition='employee_id<>:employee_id AND vehicle_id=:vehicle_id AND checked_in_on IS NULL';
$criteria->params=array(':employee_id'=>Yii::app()->session['activity']->employee_id,':vehicle_id'=>$model->id);
$checkedOutVehicleBySomeoneElse = Activity::model()->find($criteria);
Related
I using Laravel and its \Illuminate\Database\Eloquent\Builder. I would like to select all columns from "table_1" a have custom column "is_table_2_present" which value will be 1 or 0 depending if(table_1_id != null).
So I would like to do something like that.
$queryBuilder->leftJoin("table_2"....)
$queryBuilder->select([
"table_1.*",
"is_table_2_present" = (table_2_id != null) ? 1 : 0,
]);
I was trying to search for an answer but without much of a success. So I would like to ask if something like that is possible?
The reason why I cannot use Eloquent relationship is because I would need relationship with parameter. And that not possible in laravel 5.2 right?
public function table_2($userId)
{
return $this->hasOne(Table_2::class....)->where(user_id, "=", userId);
}
Conceptually each table is associated to a model. Try eloquent relationship between the models of the two table you are trying to query.
You can use selectRaw(), I think it will be:
$queryBuilder->selectRaw(
<<<EOT
table_1.*,
if(table_2_id != ?, 1, 0) as is_table_2_present,
EOT,
[null]
);
In the above code, I used binding to avoid the SQL injection.
Since this question kinda died, no respons for a while I solved it with selectRaw() for now. But still in search for more neat solution.
$queryBuilder->selectRaw("
table_one.*,
CASE WHEN table_two.id IS NOT NULL THEN 1 ELSE 0 END AS tableTwoPresent
");
Hy guys I need to realize a query in codigniter in which the condition is greater than.
I have a table named "magazzino" in which there are 2 columns : "quantita" and "alert" I wuold realize query as:
SELECT * FROM magazzino WHERE(quantità >alerts)
I tried in this way:
$this->db->select('*');
$this->db->from('magazzino');
$this->db->where('quantita>', 'alerts');
$query = $this->db->get();
return $query->result();
But this doesn't work.
Thanks in advance to all for your patience but they're novice with this framework!
Have a look at this example from the Codeigniter documentation:
$this->db->where('name !=', $name);
$this->db->where('id <', $id);
// Produces: WHERE name != 'Joe' AND id < 45
It looks as though you may need a space between quantita and >. Try:
$this->db->where('quantita >', 'alerts');
My final goal is I want to get plid and portletId that can be display my article(or entry with any type if it is possible).
I have sql query that return me any portlet availble for display my article.
But when I have to use dynamicQuery to get the same results, I get problem with xPath and array comparison, please help!
SELECT * FROM portletpreferences pr
WHERE pr.preferences != '<portlet-preferences />' AND pr.ownerid = 0 AND pr.portletid ilike '%_INSTANCE_%' AND pr.plid IN(
SELECT layout.plid FROM layout
WHERE layout.type_ = 'portlet' AND layout.groupid = 19 AND layout.hidden_ is false)
AND pr.portletpreferencesid IN (
SELECT pr.portletpreferencesid FROM portletpreferences pr
WHERE 'true' = ANY(xpath('//preference[name="anyAssetType"]/value/text()', XMLPARSE(DOCUMENT pr.preferences))::text[])
OR (SELECT (array(SELECT id_ FROM journalstructure))::text[]) && xpath('//preference[name="classTypeIds"]/value/text()', XMLPARSE(DOCUMENT pr.preferences))::text[] )
If you are bent upon using this same query, then use this query directly with Custom-SQL in liferay by creating custom-finders instead of using DynamicQuery. That would give you a lot of flexibility in using any type of SQL query directly.
I don't think this query can be converted to DynamicQuery, but if you do manage to convert it then please do post it here :-)
DynamicQuery is very powerful, see e.g. my answer how to find layouts with specific JournalArticles. I think your requirement is similar to this one:
Liferay: How to find all Layouts with the specific JournalArticle in AssetPublisher portlets?
I am running the following query, but no rows are returned even though a record exists that should match the query.
SELECT
*
FROM
tblsignup
WHERE
usr_email='amir#gmail.com'
AND
(status=1 or status=2)
You should try by simplifying the query (yeah...even if it's so simple)
try this
Select * from tblsignup
then
Select * from tblsignup where
usr_email = 'amir#gmail.com'
then
Select * from tblsignup where
usr_email='amir#gmail.com' and
status > 0
//I know you won't use > 0 at the end, but we want to eliminate the most cause of error we simplify by > 0 only to be easier to read
Tell us from where you start getting 0 line, this could lead us to the problem, I know I already had a problem like that with a field named "date", because date is already used by MySQL, funny MySQL still let me use that fieldname tho.
Try this:
select * from `tblsignup` where `usr_email`='amir#gmail.com' and (`status`=1 or `status`=2)
I have a feeling "status" might be reserved for something special. It might be worth a shot changing it to `status`.
Try wrapping brackets around the status column name:
SELECT *
FROM tblsignup
WHERE usr_email = 'amir#gmail.com'
AND ([status] = 1
OR [status] = 2);
EDIT
After reading your comment, why not use:
SELECT *
FROM tblsignup
WHERE usr_email = 'amir#gmail.com'
AND [status] > 0;
May it be that your column or table has case sensitive collation and the address is typed different ('Amir...')? As your query is correct SQL. You can find that with:
EXEC sp_help DatabaseName
I use codeigniter and have an issue about SELECT MAX ... I couldnot find any solution at google search...
it looks like it returns only id :/ it's giving error for other columns of table :/
Appreciate helps, thanks!
Model:
function get_default()
{
$this->db->select_max('id');
$query = $this->db->getwhere('gallery', array('cat' => "1"));
if($query->num_rows() > 0) {
return $query->row_array(); //return the row as an associative array
}
}
Controller:
$default_img = $this->blabla_model->get_default();
$data['default_id'] = $default_img['id']; // it returns this
$data['default_name'] = $default_img['gname']; // it gives error for gname although it is at table
To achieve your goal, your desire SQL can look something like:
SELECT *
FROM gallery
WHERE cat = '1'
ORDER BY id
LIMIT 1
And to utilise CodeIgniter database class:
$this->db->select('*');
$this->db->where('cat', '1');
$this->db->order_by('id', 'DESC');
$this->db->limit(1);
$query = $this->db->get('gallery');
That is correct: select_max returns only the value, and no other column. From the specs:
$this->db->select_max('age');
$query = $this->db->get('members');
// Produces: SELECT MAX(age) as age FROM members
You may want to read the value first, and run another query.
For an id, you can also use $id = $this->db->insert_id();
See also: http://www.hostfree.com/user_guide/database/active_record.html#select
CodeIgniter will select * if nothing else is selected. By setting select_max() you are populating the select property and therefore saying you ONLY want that value.
To solve this, just combine select_max() and select():
$this->db->select('somefield, another_field');
$this->db->select_max('age');
or even:
$this->db->select('sometable.*', FALSE);
$this->db->select_max('age');
Should do the trick.
It should be noted that you may of course also utilize your own "custom" sql statements in CodeIgniter, you're not limited to the active record sql functions you've outlined thus far. Another active record function that CodeIgniter provides is $this->db->query(); Which allows you to submit your own SQL queries (including variables) like so:
function foo_bar()
{
$cat = 1;
$limit = 1;
$sql = "
SELECT *
FROM gallery
WHERE cat = $cat
ORDER BY id
LIMIT $limit
";
$data['query'] = $this->db->query($sql);
return $data['query'];
}
Recently I have been utilizing this quite a bit as I've been doing some queries that are difficult (if not annoying or impossible) to pull off with CI's explicit active record functions.
I realize you may know this already, just thought it would help to include for posterity.
2 helpful links are:
http://codeigniter.com/user_guide/database/results.html
http://codeigniter.com/user_guide/database/examples.html