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, '%')"));
Related
I'm just taking a look at the following query
select * from tablename
where id like '%%';
So that it can handle parameters to include all of the data or filtered data like bellow
select * from tablename
where id like '%1%';
Which is fine for most parameters I use but this seems wrong for an ID because it will return all data that has IDs containing 1 which I don't want
To get around this I can only append the where clause if the ID is given but that seems like a pain in the butt
Is it possible to use a different type of where clause so that a wildcard can be used in a where equals clause instead of a where like clause, example
select * from tablename
where id = '*';
So that the same query can be used to return all or filtered data? Pass parameter '*' for all or parameter '1' for ID 1 specifically
(I'm not sure if it matters for this case but I'm using PostgreSQL 9.6.12 in this example)
This would often be expressed as:
where (id = :id or :id is null)
null is the "magic" value that represents all rows.
I'm trying to compare two columns from a table. In which i have to check the email is containing his mobile number or not.
TableName:- Table1
TableColumns:- id,email,MOB
Ex.
SQL:
'SELECT * FROM Tabel1 WHERE email LIKE %MOB%'
Laravel :
Tabel1::whereColumn('email', 'LIKE','%MOB%')->get();
I have Tried this above query but it is showing syntax error.
Lets start by answering you question SQL-wise, anything in quotes is a literal to SQL, so you need to use column reference and add the wildcard symbols. You can do that like this:
SELECT * FROM Table1 WHERE email LIKE CONCAT('%', MOB, '%');
Now lets look at Laravel now, the 3rd argument to where expects a literal value not another column. You can overcome this via either whereRaw or DB::raw:
Table1::whereRaw("email LIKE CONCAT('%', MOB, '%')");
or
Table1::where('email', 'LIKE', DB::raw("CONCAT('%', MOB, '%')"));
You should try this
SELECT * FROM Tabel1 WHERE email LIKE '%' +MOB+'%'
SELECT * FROM `table` WHERE `email` LIKE '%MOB%';
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");
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.
I'm trying to make a search feature that will search multiple columns to find a keyword based match. This query:
SELECT title FROM pages LIKE %$query%;
works only for searching one column, I noticed separating column names with commas results in an error. So is it possible to search multiple columns in mysql?
If it is just for searching then you may be able to use CONCATENATE_WS.
This would allow wild card searching.
There may be performance issues depending on the size of the table.
SELECT *
FROM pages
WHERE CONCAT_WS('', column1, column2, column3) LIKE '%keyword%'
You can use the AND or OR operators, depending on what you want the search to return.
SELECT title FROM pages WHERE my_col LIKE %$param1% AND another_col LIKE %$param2%;
Both clauses have to match for a record to be returned. Alternatively:
SELECT title FROM pages WHERE my_col LIKE %$param1% OR another_col LIKE %$param2%;
If either clause matches then the record will be returned.
For more about what you can do with MySQL SELECT queries, try the documentation.
If your table is MyISAM:
SELECT *
FROM pages
WHERE MATCH(title, content) AGAINST ('keyword' IN BOOLEAN MODE)
This will be much faster if you create a FULLTEXT index on your columns:
CREATE FULLTEXT INDEX fx_pages_title_content ON pages (title, content)
, but will work even without the index.
1)
select *
from employee em
where CONCAT(em.firstname, ' ', em.lastname) like '%parth pa%';
2)
select *
from employee em
where CONCAT_ws('-', em.firstname, em.lastname) like '%parth-pa%';
First is usefull when we have data like : 'firstname lastname'.
e.g
parth patel
parth p
patel parth
Second is usefull when we have data like : 'firstname-lastname'. In it you can also use special characters.
e.g
parth-patel
parth_p
patel#parth
Here is a query which you can use to search for anything in from your database as a search result ,
SELECT * FROM tbl_customer
WHERE CustomerName LIKE '%".$search."%'
OR Address LIKE '%".$search."%'
OR City LIKE '%".$search."%'
OR PostalCode LIKE '%".$search."%'
OR Country LIKE '%".$search."%'
Using this code will help you search in for multiple columns easily
SELECT * FROM persons WHERE (`LastName` LIKE 'r%') OR (`FirstName` LIKE 'a%');
Please try with above query.