laravel fetch query skip data with column if condition - mysql

here is a query for fetch data from DB
$subcatgeory = DB::table('subategories')->where('category_id',$item->id)->get();
in a subcategory, there is a column name for subcategory_En values are look like this
subcategory_En
sample data
sample data 2
unassign
i want to skip when the subcategory_En column value is unassign and fetch other data
so I try this query
$subcatgeory = DB::table('subategories')->where('category_id',$item->id)->skip('subcategory_En','unassign')->get();
this query is not working how can I fetch data and skip with column values?

Just do a second where clause.
$subcatgeory = DB::table('subategories')->where('category_id',$item->id)->where('subcategory_En', '!=', 'unassign')->get();
Skip is for limits and offsets.
https://laravel.com/docs/8.x/queries#limit-and-offset

Related

Fetch data from same named two columns of two tables

I am trying to fetch data from 2 tables with a join query. Here I have 2 columns in 2 tables with same column name.
This is my query:
public function get_all_expenses()
{
$this->db->select("*",'category.name as cat_name');
$this->db->from('expense');
$this->db->join('category','expense.cat_id = category.id');
$this->db->join('users','expense.user_id = users.id');
$query = $this->db->get();
return $query;
}
I can fetch data of 1 column of 1 table. But I can't fetch data of another column of another table. I am using CodeIgniter.
According to the CodeIgniter documentation the database select method accept a single argument. The correct syntax for the select is then:
$this->db->select('*, category.name as cat_name');

No database selected Select the default DB to be used by double-clicking

I am writing a SQL query and I have an array in one table and in that array I stores IDs and want to compare that array of IDs with another table to show data against that ids. when I run it gives me following error.
No database selected Select the default DB to be used by double-clicking.
Here is my query
select TagId
, Name
from ctrData2.Tag
Left
outer join ctrData2.CallDetail
On Tag.TagId = array(CallDetail.Tag)
where CallDetail.ContactId = 'f9d4787a-f1ac-41af-97d8-ea324daad018'
this is how I store IDs in array in Tag Column
here is the other table from where I want to show data against these ids
you can set the default schema by below query first
use ctrData2;
And run below query
select TagId, Name
from Tag, CallDetail
where CallDetail.ContactId = 'f9d4787a-f1ac-41af-97d8-ea324daad018' and LOCATE(Tag.TagId, CallDetail.Tag) > 0;

Azure table storage - where clause on column that may not exist

I am adding a new column to my azure table. For ex., the table is called 'User' and the new column is called 'ComputationDate'. The 'User' table already exists with rows that do not have this new column 'ComputationDate'. I have a query over it as follows,
var usersDue = from user in Table.Query
where user.ComputationDate != <somedate>
select user;
I want this query to return all user rows that do not have ComputationDate set to 'somedate' and also user rows that do not have this new 'ComputationDate' column defined.
But the query doesn't return the latter set of users. It only returns rows that have 'ComputationDate' set and where the value is not equal to 'somedate'.
Is there any way to get the results I desire without having to get all users and filter it on the client?
It looks like you're trying to do a LINQ to SQL query.
This may serve your needs better:
var usersDue = from user in Table.Query
where user.ComputationDate != <somedate>
|| user.ComputationDate == null
select user;

codeigniter ActiveRecord where statement issue

is have this statement and i want to make it by the Active Records way in codeigniter
DELETE FROM TABLE
(col1 = value AND col2 = value2 ) OR (col1 = value2 AND col2 = value );
CodeIgniter Active Record is impractical for mixing AND's and OR's within a query.
You will likely need to construct some of the query manually, e.g. something along the lines of:
$this->db->where("col1 = $value AND col2 = $value2");
$this->db->or_where("col1 = $value2 AND col2 = $value");
Alternately, in your example where there are only two specific columns and two values, the following idea should also work:
$this->db->where_in('col1', array($value, $value2));
$this->db->where_in('col2', array($value, $value2));
$this->db->where('col1 != col2');
Try this:
$this->db->where('col1', $value1);
$this->db->where('col2', $value2);
$this->db->delete($table);
That's not the goal of active record. As name suggest active record object instance is tied to a single row in a table. If you want to do this in "active record way" you have to retrieve all the records you have to delete the database, mark them as deleted and commit changes witch is overkill. Best you can do is to get connection instance from codeignither and execute query manually.

MySQL update with two subqueries

I'm trying to update one column of MySQL table with subquery that returns a date, and another subquery for the WHERE clause.
Here is it:
UPDATE wtk_recur_subs_temp
SET wtk_recur_date = (SELECT final_bb.date
FROM final_bb, wtk_recur_subs
WHERE final_bb.msisdn = wtk_recur_subs.wtk_recur_msisdn)
WHERE wtk_recur_subs_temp.wtk_recur_msisdn IN (select final_bb.msisdn
from final_bb)
The response from the MySQL engine is "Subquery returns more than 1 row".
Use:
UPDATE wtk_recur_subs_temp,
final_bb,
wtk_recur_subs
SET wtk_recur_subs_temp.wtk_recur_date = final_bb.date
WHERE final_bb.msisdn = wtk_recur_subs.wtk_recur_msisdn
AND wtk_recur_subs_temp.wtk_recur_msisdn = final_bb.msisdn
The error is because:
SET wtk_recur_date = (SELECT final_bb.date
FROM final_bb, wtk_recur_subs
WHERE final_bb.msisdn = wtk_recur_subs.wtk_recur_msisdn)
...the final_bb.date value is all the date values where the final_bb and wtk_recur_subs msisdn column values match.
This may come as an utter shock to you, but one of your subqueries is returning more than one row!
This isn't permitted in the circumstance you've set up. Each of those two subqueries must return one and only one row. Or no rows.
Perform each subquery on it's own and determine which one is returning more than one row. If they shouldn't return more than one row, your data may be wrong. If they should return more than one row, you'll either want to modify the data so they don't (as I assume you expect), or add a LIMIT clause. Or add an aggregate function (like MAX) outside the query to do something proper with the multiple rows being returned.