Unfortunately the Laravel query builder seems not in a good mood!
Here is my table structure:
Table user structure
ID, USERNAME, NAME, USER_ROLE_MODULE_ID
Table user_role_module structure
ID, NAME
And this is my query builder statement
DB::table('user')
->leftJoin('user_role_module', 'user.user_role_module_id', '=', 'user_role_module.id')
->select('user.*', 'user_role_module.name as user_role')
->where("name LIKE '%Jhon%' ") "or other example" ->where("user_role LIKE '%admin%' ")
->paginate(10);
Goal:
I just want to get all data from user + user role name from user_role_module AND also applies in WHERE statement so I can use it for search feature.
I am getting result from WHERE statement without specifying table name. because it already stated which table and column to select from SELECT statement.
The problem:
if I search for name, it return error ambiguous column //Laravel is confusing whether taking name from user table or user_role_module table!
if I search for user_role, then the column doesn't exist
Why is it? What is the solution?
The problem:
user.NAME <-- both these columns are called NAME
user_role_module.NAME <--
You have multiple problems with your Laravel query. As #Viktor correctly pointed out, your join condition is wrong. You should be joining user.USER_ROLE_MODULE_ID to user_role_module.id. But the immediate cause of your error I believe is that both tables have a NAME column.
Just specify the columns you want using both table and column name. This way, no column name would be ambiguous.
DB::table('user')
->leftJoin('user_role_module', 'user.USER_ROLE_MODULE_ID', '=', 'user_role_module.id')
->select('user.ID', 'user.USERNAME', 'user.NAME AS userName',
'user_role_module.name as user_role')
->where("name LIKE '%John%'")
->paginate(10);
Change this:
DB::table('user')
->leftJoin('user_role_module', 'user.user_role_module_id', '=', 'user_role_module.id')
->select('user.*', 'user_role_module.name as user_role')
->where("name LIKE '%Jhon%' ") "or other example" ->where("user_role LIKE '%admin%' ")
->paginate(10);
with this:
DB::table('user')
->leftJoin('user_role_module', 'user.user_role_module_id', '=', 'user_role_module.id')
->where('user.name', 'like', '%Jhon%')
->select('user.*', 'user_role_module.name as user_role')
->get();
and make a custom pagination because select is not working with paginate.
Related
I apologize if this is duplicated. Is it possible in Eloquent ORM to select records where one field equals another? In MySQL this means:
SELECT Id, Url, ModelId
WHERE Url LIKE CONCAT('%', ModelId, '%')
Is it possible to do so in Eloquent or I'm bound to using raw queries?
On field equalling another in the query builder (and eloquent) is whereColumn('Url', 'LIKE', 'ModelId') but since you have additional things in the second part you will need to use raw queries like e.g.
DB::table('table')
->select('Id', 'Url', 'ModelId')
->where('Url', 'LIKE', DB::raw("CONCAT('%', ModelId, '%')"));
I am using POSTGRESQL with sequelize.js and would really like to execute a query which looks like this
SELECT "CT_Foo"."cola", "CT_Foo"."colb", "CT_Foo"."colc",
"CT_Foo"."cold", "CT_Foo"."cole", "CT_Foo"."colf",
COUNT(*)
FROM (
SELECT "CT_BAR"."cola", "CT_BAR"."colb", "CT_BAR"."colc",
"CT_BAR"."cold", "CT_BAR"."cole", "CT_BAR"."colf",
"CT_BAR"."colg"
FROM public."Customers" AS "CT_BAR"
WHERE ("CT_BAR"."colf" IN ('SOMEID') AND "CT_BAR"."colg" ?
'date')
) AS "CT_Foo"
WHERE ("CT_Foo"."colf" IN ('SOMEID'))
GROUP BY "CT_Foo"."cola", "CT_Foo"."colb", "CT_Foo"."colc",
"CT_Foo"."cold", "CT_Foo"."cole", "CT_Foo"."colf"
Columns A-F are text and G is JSONB
Basically the reason why I am doing this is because I need to group on columns A-F with a query on column F and if something exists in the JSONB in column G, I do not wish to include column G in the grouping, because it's JSON and I'm checking for the existence of a date... This seems a simple way to do this. I know postgresql has CTE's but sequelize does not support them. I believe another name for this is "Derived Tables"
I can form a query normally, but cannot get the subquery into the FROM clause.
Any idea on how todo this or get the same result?
I have a table vehicles has a column 'name'
Values are stored like car/tesla, car/honda, truck/daimler (each value is stored as type/brand)
I want to query the table using only brand. If I look up tesla, it should return the row corresponding to car/tesla. How do I do it? I'm using postgres.
There is no need in regex in your case. Just use old good like:
select name
from vehicles
where name like '%/tesla'
2 solutions are available a select query with LIKE operand or a select query with contains operand.
select * from vehicles where name LIKE '%tesla%'
Select * from vehicles where Contains(name, "tesla");
The following is my sql statement for a join in dB2.
select name, address, bloodgroup
from user_tb, health_tb
where user_tb.id = health_tb.id;
I am getting the following error:
"health_tb.id" is not valid in the context where it is used..
SQLCODE=-206, SQLSTATE=42703, DRIVER=4.12.79
I understand that one reason why I could be getting this error is because id may not exist in health_tb, but that is not the case. I hope someone can advise. Thank you.
First, you should learn to use modern join syntax, although this has nothing to do with your problem:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.id;
A simple search on Google pointed me to the documentation for this error. One of the first things it mentions is:
Possible reasons for this error include:
The specified column is not a column of any of the source or target
tables or views of the statement.
In a SELECT or DELETE statement, the specified column is not a column of any of the tables or views that are identified in a FROM
clause in the statement.
A column list of an SQL data change statement specified the name of a column of the target table or view of the statement.
I suspect that the id column is really called something like user_id. The working query might look like:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.user_id;
1) check if the id column in both tables have the same data type
2) check if there is any trailing space in the column name
select '<' || column_name || '>' from user_tab_columns
where tbname = 'health_tb'
If the id columns are defined as different types, that could be a problem.
I'm trying to search based on full_name, which is a concatenation of first_name and last_name, but I keep getting an error.
This is what my controller looks like:
$criteria = new CDbCriteria;
$criteria->addSearchCondition('full_name',$customer_search);
$customers = Customer::model()->findAll($criteria);
In my Customer model, I have a method that should return the full_name:
public function getFull_name() {
return $this->first_name.' '.$this->last_name;
}
But, I get this error:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'full_name' in 'where clause'. The SQL statement executed was: SELECT * FROM `customer` `t` WHERE full_name LIKE :ycp0
I need to be able to search both the first_name and last_name, what do I need to change to make this work?
You cannot use "calculated" columns in your query. You will simply have to rewrite the criteria so that they only check columns that physically exist in the database.
Since you want to find people where part of either their first or last name matches you will have to enclose the search term in percent signs (see MySql documentation for LIKE) and specify the second condition with OR:
$criteria = new CDbCriteria;
$criteria->addSearchCondition('first_name', $customer_search);
$criteria->addSearchCondition('last_name', $customer_search, true, "OR");
$customers = Customer::model()->findAll($criteria);