I basically want to do:
select * from request where id = 1 and created_at like (today's date);
but using eloquent.
I tried:
$query = m_requests::where(['id' => Auth::User()->id, "created_at", "like", "%".date("Y-m-d")."%"]);
but that returns the error:
SQLSTATE[42S22]: Column not found: 1054 Unknown
column '0' in 'where clause' (SQL: select * from
from `request` where (`id` = 1 and `0` = created_at
and `1` = like and `2` = %2016-04-22%))
Does anyone know how I could achieve what I am trying to do?
In summary:
I want the id to equal the id requested, but I want the date to be "LIKE" today's date (This is because I'm only interested in the date it was created and not the time it was created).
Cheers
Try this, it should work:
m_requests::where('id', '=', Auth::user()->id)
->where('created_at', 'LIKE', '%'.date("Y-m-d").'%');
Related
I have a working mysql query like this:
SELECT mc.cart_id, mc.mystore_user_id, MIN(ci.created_at) AS created_at
FROM dmspro_mys_cart AS mc
INNER JOIN dmspro_mys_cart_item AS ci
ON ci.cart_id = mc.cart_id
WHERE mc.is_noticed = 0 AND ci.created_at < '2019-10-08 07:08:39'
GROUP BY mc.cart_id
And I converted it to query builder in my Laravel project:
public function getMinCreatedAt($cartTime)
{
$oSelect = $this->select("{$this->table}.cart_id", "{$this->table}.mystore_user_id",\DB::raw('MIN(ci.created_at) AS created_at'))
->join('cart_item AS ci', 'ci.cart_id', '=', "{$this->table}.cart_id")
->where("{$this->table}.is_noticed", '=', 0)
->where('ci.created_at', '<', $cartTime)
->groupBy("{$this->table}.cart_id")
->get();
return $oSelect;
}
But when I run this, I got error:
Column not found: 1054 Unknown column 'ci.created_at' in 'field list'
(SQL: select dmspro_mys_cart.cart_id,
dmspro_mys_cart.mystore_user_id, MIN(ci.created_at) AS created_at
from dmspro_mys_cart inner join dmspro_mys_cart_item as
dmspro_mys_ci on dmspro_mys_ci.cart_id =
dmspro_mys_cart.cart_id where dmspro_mys_cart.is_noticed = 0
and dmspro_mys_ci.created_at < 2019-10-09 15:51:37 group by
dmspro_mys_cart.cart_id)
How I can fix this?
Thank you!
UPDATE: dmspro_mys_ is my prefix
Your error message does not align with your query.
In the error message an alias named "dmspro_mys_ci" is mentioned, but it does not exist in your query.
Can you please double check this?
Basically your are debuggen the wrong query.
Update
As laravel is setup with prefixing database tables, the alias "ci" is also prefixed.
When referencing this alias in a DB::raw() the table/alias will not automatically be prefixed, so you have to do that yourself by changing:
\DB::raw('MIN(ci.created_at) AS created_at')
to:
\DB::raw('MIN(dmspro_mys_ci.created_at) AS created_at')
Im going to count my pallet_condition 0,1,2,3 and group it by month
but i always got an error like this please help whats wrong with my code.
something wrong with my date_format?
My Error
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in group statement is ambiguous (SQL: select count(*) as aggregate from `liip_psrm` inner join `liip_psrm_items` on `liip_psrm`.`id` = `liip_psrm_items`.`psrm_items_id` where date(`liip_psrm_items`.`created_at`) >= 2018-10-09 and date(`liip_psrm_items`.`created_at`) <= 2019-01-31 and `liip_psrm`.`status` = 0 and `liip_psrm`.`customer_id` = 14 and `pallet_condition` = 0 group by WEEK(created_at) order by WEEK(created_at) asc)
My Callback function
$problem_condition_computations = function($condition) use ($psrm_maintenance, $start, $end, $customers){
return DB::table('liip_psrm')
->join('liip_psrm_items', 'liip_psrm.id', '=', 'liip_psrm_items.psrm_items_id')
->whereDate('liip_psrm_items.created_at', '>=', $start)
->whereDate('liip_psrm_items.created_at', '<=', $end)
->select(
DB::raw("DATE_FORMAT(liip_psrm_items.created_at, '%Y-%m-%d') AS dates
"))
->where('liip_psrm.status','=', 0)
->where('liip_psrm.customer_id','=',$customers)
->where('pallet_condition', $condition)
->groupBy('dates')
->count();
};
"dates" field you are using in groupBy is alias name, So it do not identifying it and not working as you are expecting.
Same as you used in Select
DB::raw("DATE_FORMAT(liip_psrm_items.created_at, '%Y-%m-%d') AS dates")
Put it in GroupBy
& It will work.
Hope, It will help, Let me know if you still find difficulties.
I want to get last 7 quarters data from database using created_at value from table that you will come to know by seeing my code below.
BTW this code works for days,months and year by replacing quarter to one of this words.While running this code I got following error
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'quarter' in
'where clause' (SQL: select client_accounts.* from client_accounts
inner join leads on client_accounts.clients_id =
leads.clients_id inner join associates on
leads.associates_id = associates.id where quarter =
client_accounts.created_at and associates.users_id = 1) ◀"
for ($i = 0; $i < 7; $i++)
{
$usersquarter[] = ClientAccount::wherequarter('client_accounts.created_at', '=', Carbon\Carbon::now()->subQuarter($i)->quarter)
->select('client_accounts.*')
->join('leads', 'client_accounts.clients_id', '=', 'leads.clients_id')
->join('associates', 'leads.associates_id', '=', 'associates.id')
->where('associates.users_id', '=', $associateid)
->get();
}
I am working on some maintaince project where all are sql queries but i want to convert it in to laravel. Here is the below query :-
$members = Member::select('members.id','members.first_name',
'members.surname','members.username','members.password',
'members.email','user_access.active',
DB::raw('SUM( IF (user_access.active = "y", 1, 0) ) as acount'))
->join('user_access','user_access.member_id','=','members.id')
->where('special_access','=','n')
->groupby('user_access.member_id')
->having('acount','>','0')
->orderby('members.id','desc')
->orderby('members.username','ASC')
->orderby('user_access.note','DESC')
->paginate(30);
Its giving me error when i execute the query. below the error in having clause
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'acount' in 'having clause' (SQL: select count(*) as aggregate from `members` inner join `user_access` on `user_access`.`member_id` = `members`.`id` where `special_access` = n group by `user_access`.`member_id` having `acount` > 0)
I guess something like this will help you.
$userAccess = DB::table('user_access')
->where('special_access','=','n')
->where('active','y')
->groupby('member_id');
Member::select([
'members.id',
'members.first_name',
'members.surname',
'members.username',
'members.password',
'members.email',
'user_access.active'
])->joinSub($userAccess, 'user_access', function($join){
$join->on('user_access.member_id', '=', 'members.id');
})->orderby('members.id','desc')
->orderby('members.username','ASC')
->orderBy('user_access.note','asc')
->paginate(30);
OR
$members = Member::select([
'members.id',
'members.first_name',
'members.surname',
'members.username',
'members.password',
'members.email',
'user_access.active'
])->join('user_access', function($join){
$join->on('user_access.member_id', '=', 'members.id')->on('user_access.active', '=', DB::raw('"y"'));
})
->where('special_access','=','n')
->groupby('user_access.member_id')
->orderby('members.id','desc')
->orderby('members.username','ASC')
->orderby('user_access.note','DESC')
->paginate(30);
As apokryfos said in the comments:
The paginator will attempt to get the count of the query and will remove all selects from it when doing so leading to this error.
If you just need records with user_access.active = "y" then you do not need to select them in the first place and then try to filter them out by HAVING
I'm using ZF2 DB Adapter and get following error on my query:
Column not found: 1054 Unknown column '"product"'
But "product" is no column, it's a value, so how come?
This is how I build the query:
$select = $this->getGateway()->getSql()->select();
$select->join('keywordlink', 'keywordlink_ref_type = "product" AND keywordlink_ref_id = product_id', ['keyword_count' => new Expression('COUNT(keyword_id)')], Select::JOIN_LEFT);
$select->where(['product_deleted IS NULL']);
$select->group(['product_id']);
Btw, the field keywordlink_ref_id is an ENUM in the mysql database.
When I write the SQL myself it works:
SELECT
product.*, COUNT(DISTINCT keywordlink_keyword_id) AS keyword_count
FROM
adcheck.product
LEFT JOIN
keywordlink
ON
keywordlink_ref_type = "product" AND keywordlink_ref_id = product_id
WHERE
product_deleted IS NULL
GROUP BY
product_id
Thanks
Ok, I don't know why it didn't work as before but it works when I move the
keywordlink_ref_type = "product"
into the where condition:
SELECT
product.*, COUNT(DISTINCT keywordlink_keyword_id) AS keyword_count
FROM
adcheck.product
LEFT JOIN
keywordlink
ON
keywordlink_ref_id = product_id
WHERE
keywordlink_ref_type = "product" AND product_deleted IS NULL
GROUP BY
product_id
$sql = $this->getGateway()->getSql()->select();
$sql->from('product')->join('keywordlink', "keywordlink.ref_id" = "product.id", LEFT);
$sql->where->equalTo( 'keywordlink.column' = 'product.column');
$sql->where->equalTo('product.deleted', NULL);
$sql->group('product.product_id');
The column wasn't found because not pointing table in your query.The solution something like this,but for proper syntax of ZF2 query builder you should also check out this zend2 link: http://framework.zend.com/manual/1.12/en/zend.db.select.html