I have the following query:
$query = $qb->select($qb->expr()->substring("p.website",1,5).'AS country)
->from("AppBundle\Entity\Image" ,"p")
->where("p.aktuellste = 1")
->andWhere($qb->expr()->in('country',':country'))
->setParameter(':country','de')
->orderBy('country','DESC')
->getQuery();
E.g. I would like to select all lines where substring(1,2) of website is de. But country is an unknown column for Doctrine in the WHERE clause. The following exception is thrown:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sclr_0' in
'where clause
The funny thing is that Doctrine knows the country column in the ORDER BY clause.
Does anybody know how to fix this?
If you want to search on an aliased column, you need to use HAVING, because that is on a calculated field. Putting it in your WHERE clause is assuming that your Image entity has a member named country in it, which it does not.
$query = $qb
->select($qb->expr()->substring('p.website', 1, 5).'AS country')
->from('AppBundle\Entity\Image', 'p')
->andWhere('p.aktuellste = 1')
->andHaving($qb->expr()->in('country', ':country'))
->setParameter(':country', 'de')
->orderBy('country', 'DESC')
->getQuery()
;
See MySQL Handling of Group By for more information:
The MySQL extension permits the use of an alias in the HAVING clause
for the aggregated column
There are some syntax errors in your query, but not sure this fixes your problem.
Please try this:
$query = $qb->select($qb->expr()->substring("p.website",1,5).'AS country')
->from("AppBundle\Entity\Image" ,"p")
->where("p.aktuellste = 1")
->andWhere($qb->expr()->in('country=:country'))
->setParameter('country','de')
->orderBy('country','DESC')
->getQuery();
Related
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'vpc_order_info' in 'where clause' (SQL: select `sequa_transaction_id`
as `vpc_transaction_no`, `merchant_order_id` as `vpc_order_info`,
`date_created` as `yesterday`, `card_number` as `vpc_Card`, `amount`
as `vpc_Amount`, `authorization_code` as `vpc_authorizeId` from
`ipg_sentry_response` where `merchant_id` = 12136901 and
`vpc_order_info` like %-% and `response_code` = 1 and `date_created`
between 2016-03-22 and 2016-09-31)
I am getting this error message though I have defined the column as given below.
$col = array("sequa_transaction_id as vpc_transaction_no","merchant_order_id as vpc_order_info","date_created as yesterday","card_number as vpc_Card", "amount as vpc_Amount","authorization_code as vpc_authorizeId");
$records = DB::table('ipg_sentry_response')->select($col)
->where('merchant_id', '=', $vpc_Merchant)
->where('vpc_order_info' ,'like', "%-%")
->where('response_code', '=', '1')
->whereBetween('date_created', array($date,$date2))
//->whereBetween('date_created', array($date,$date2))
->get();
Can anyone please help me with it? I am using Laravel version 5.2.
The column alias is defined but it is not available to be used in where caluse yet. You can use alias in group, order... you need to use the column name instead of the alias. Try -
$col = array("sequa_transaction_id as vpc_transaction_no",
"merchant_order_id as vpc_order_info","date_created as yesterday",
"card_number as vpc_Card", "amount as vpc_Amount",
"authorization_code as vpc_authorizeId");
$records = DB::table('ipg_sentry_response')->select($col)
->where('merchant_id', '=', $vpc_Merchant)
->where('merchant_order_id' ,'like', "%-%")
->where('response_code', '=', '1')
->whereBetween('date_created', array($date,$date2))
->get();
You can only use column aliases in GROUP BY, ORDER BY, or HAVING clauses. Standard SQL doesn't allow you to refer to a column alias in a WHERE clause.
As what stated by the answer above mine, the WHERE clause doesn't accept aliases. Furthermore, you may also use the GROUP BY - HAVING methods (https://laravel.com/docs/5.3/queries#ordering-grouping-limit-and-offset)
I'm trying to fetch records with an array of exceptions, here's what I tried (refer below)
$users_nowishlist = DB::table('employee')
->join('users', 'users.employee_id', '=', 'employee.employee_id')
->where('has_wishlist', '=', "0")
->whereNotIn('employee_id', ['MMMFLB003', 'guest_01', 'guest_02', 'guest_03'])
->where('employment_status', '=', 'ACTIVE')
->get();
so in this line was my records filter, means only records that does not equal to any of those 'employee_id' from the exceptions array will be return (refer below)
->whereNotIn('employee_id', ['MMMFLB003', 'guest_01', 'guest_02', 'guest_03'])
but instead I got this error (refer below):
SQLSTATE[23000]: Integrity constraint violation: 1052 Column
'employee_id' in where clause is ambiguous (SQL: select * from
employee inner join users on users.employee_id =
employee.employee_id where has_wishlist = 0 and employee_id
not in (MMMFLB003, guest_01, guest_02, guest_03) and
employment_status = ACTIVE)
any ideas, help please?
This happens because when you are doing the join there are two columns with the same name.
That's why on your join you prefix the employee_id with users. and employee.
Now on your whereNotIn you also have to prefix it, so the query engine knows which table column you are trying to reference. So you only have to add the prefix in your whereNotIn clause:
->whereNotIn('employee.employee_id', ['MMMFLB003', 'guest_01', 'guest_02', 'guest_03'])
->whereNotIn('employee.employee_id', ['MMMFLB003', 'guest_01', 'guest_02'])
when using join , these errors are expected if you have two fields have the same name in the tables you join between, so always try to fetch them like this
table_name.field_name
I have two tables in Laravel connected with a pivot table. The two tables are users and roles, and the pivot table is called role_user. The pivot table also contains two extra fields: start and stop. This way I can track which roles a user has had in the past.
Now I want to create a query that gets all users who currently have role_id = 3.
First I had used WherePivot, but apparently that is bugged.
I have now made the following query using Eloquent:
Role::with('User')
->where('id', '=', '3')
->where('role_user.start', '<', date('Y-m-d'))
->where('role_user.stop', '>', date('Y-m-d'))
->whereHas('users', function($q){
$q->where('firstname', 'NOT LIKE', '%test%');
})
->get();
But somehow I am getting an error that the column start of the pivot table cannot be found. But I can confirm in PHPMyAdmin that the column is there.
This is the entire error:
Illuminate \ Database \ QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'klj_role_user.start' in 'where clause' (SQL: select * from `klj_roles` where `id` = 3 and `klj_role_user`.`start` < 2014-06-02 and `klj_role_user`.`stop` > 2014-06-02 and (select count(*) from `klj_users` inner join `klj_role_user` on `klj_users`.`id` = `klj_role_user`.`user_id` where `klj_role_user`.`role_id` = `klj_roles`.`id` and `firstname` NOT LIKE %test%) >= 1)
Can someone tell me if I am doing something wrong or give me a hint where I should be looking now?
The error is telling you that you are missing the start column in your pivot table klj_role_user. What you should do is create the column. If the column is already there, ensure you are using the correct database.
I've also simplified your query a little bit. You don't really need a whereHas because you aren't trying to limit your roles by the users associated, but by the id, which in this case, you are using 3. A with() would work perfectly fine and wherePivot() seems to be working fine for me when used in conjunction with with().
$role = Role::with(array('users' => function($q)
{
$q->wherePivot('start', '>', date('Y-m-d H:i:s'));
$q->wherePivot('stop', '<', date('Y-m-d H:i:s'));
$q->where('firstname', 'NOT LIKE', '%test%');
}))->find(3);
foreach($role->users as $user) {
echo $user->firstname;
}
my tables (structures, surface) have two Index-Rows, "planet_id" and "tile_id". I want to join them, but i get a SQL-Error: "Column 'planet_id' in where clause is ambiguous".
$this->db ->select('*')
->from('structures')
->join('surface', 'structures.planet_id=surface.planet_id AND structures.tile_id=surface.tile_id')
->where('planet_id', $p->planet_id);
$query = $this->db->get();
Leads to:
Error Number: 1052
Column 'planet_id' in where clause is ambiguous
SELECT * FROM (`structures`) JOIN `surface` ON `structures`.`planet_id`=`surface`.`planet_id` AND structures.tile_id=surface.tile_id WHERE `planet_id` = '13247'
Since you have planet_id in two tables, you'll need to choose which you're applying the where to.
So, try this:
$this->db->select('*')
->from('structures')
->join('surface', 'structures.planet_id=surface.planet_id AND structures.tile_id=surface.tile_id')
->where('structures.planet_id', $p->planet_id);
$query = $this->db->get();
It might seem silly, because your join requires both planet_id's to be the same, but the where doesn't know that, and needs specific instructions.
I have the following query:
return Doctrine_Query::create()
->from('Model_Article m')
->where($where)
->orderBy($order);
In $where I have this:
$where='m.title='.$name;
The above produces this error:
Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sadsa' in 'where clause'. Failing Query: "SELECT COUNT(*) AS num_results FROM article a WHERE a.title = sadsa"
Why?
Error is because resulting query looks like "** where title=sadsda **", so SQL engine looks for column named "sadsda". To prevent it you must mention, that you want to compare with a string, not a column value. You can make it using your engine rules (usually enclose string with "'"), but it isn't secure, I think you should use placeholders engine, that doctrine provides, for example
$whereKey = 'm.title=?';
$whereValue = 'sadsda';
Doctrine_Query::create()->from('Model_Article m')->where($whereKey,$whereValue)->orderBy($order);