Compare two columns with 'LIKE' Operator in SQL / Laravel - sql

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%';

Related

ABAP LIKE with select option

I have the following abap program that looks as following:
TABLES lfa1.
DATA gt_lfa1 TYPE SORTED TABLE OF lfa1 WITH UNIQUE DEFAULT KEY.
SELECT-OPTIONS sl_lifnr FOR lfa1-lifnr.
SELECT-OPTIONS sl_name FOR lfa1-name1.
START-OF-SELECTION.
SELECT * FROM lfa1
INTO CORRESPONDING FIELDS OF TABLE gt_lfa1
WHERE lifnr IN sl_lifnr
AND name1 LIKE sl_name.
Searching for vendors that name starts with:
I've got no results, but it exists vendors with this pattern.
It's not necessary to use LIKE. You can use IN instead.
I ran your code with IN in the SQL and I have results in the table.
if you use a select option you have to use the statement IN not like:
s_matnr = *3578 *
select options s_matnr type mara-matnr.
select bismt
from mara
into lv_bismt
where matnr in s_matnr
If you use s/4hana you can use even fuzzy
You need to replace * with %, if you are using LIKE operand. https://help.sap.com/doc/abapdocu_750_index_htm/7.50/de-DE/abenwhere_logexp_like.htm
REPLACE ALL OCCURENCES OF '*' in sl_lifnr WITH '%'
REPLACE ALL OCCURENCES OF '*' in sl_name WITH '%'
SELECT * FROM lfa1
INTO CORRESPONDING FIELDS OF TABLE gt_lfa1
WHERE lifnr LIKE sl_lifnr
AND name1 LIKE sl_name.

SQl how to check that a column record contain a part of anothor column record

I have a request where I need to check if all records of a column cost_code contain a string composed of column invoices records
and one character of type number.
How can I achieve this task?
I tried it with this query:
SELECT * FROM Deal WHERE cost_code LIKE ('invoice%');
You can use regular expressions:
where not regexp_like(cost_code, '^invoice[0-9]$')
CONCAT() can be used with LIKE
SELECT * FROM Deal WHERE cost_code LIKE CONCAT(invoices, '%');
If cost_code is ABCD1234 & invoices is 1234, Then you can use query as:
SELECT * FROM Deal WHERE cost_code LIKE
CONCAT( SUBSTRING( invoices, 5, length(invoices) -4), '%');

using % in IN operator of sql

I have a query like this
select * from table_employee where name in ('Jack','Jon','Jade');
and it gives me three results.
but I want to use % with the names I mentioned in query like:
select * from table_employee where name in ('%Jack%','%Jon%','%Jade%');
The query execution is completed but now I get no results. Whats wrong here and how can i use % in IN operator?
You should use LIKE combined with OR, IN does not support wildcards
select * from table_employee where name LIKE '%Jack%'
OR name LIKE '%Jon%';
The in keyword does not work like that. You can use a like operand and bind the three values with or
example
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';
Convert it to an OR:
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';
If your fields data are exact what you want get in the result is no needed to use like . You should use like only when you don't know the data in a field but you know that it could contains a string Jack for example .
I'm sayng this because if you have an index on the field where you use LIKE it will be not used , so you will get performance problem .

Sql select rows containing part of string

I want to write a comparation procedure (t-sql) for site seo.
I have a table with field 'url' (nvarchar()) that contain a part of site url's.
Ex: 'mysyte.com/?id=2'. Also this table for each url contains metadata, that i need to extract.
The main problem is that full url on site looks like 'mysyte.com/?id=2&region=0&page=1', and i just need to ignore everething, except url in table:
I mean: 'mysyte.com/?id=2' => is a part of 'mysyte.com/?id=2&region=0&page=1'
You can use the LIKE operator to compare the content of a T-SQL string, e.g.
SELECT * FROM [table] WHERE [field] LIKE '%stringtosearchfor%'
The percent character '%' is a wild card- in this case it says return any records where [field] at least contains the value "stringtosearchfor".
SELECT *
FROM myTable
WHERE URL = LEFT('mysyte.com/?id=2&region=0&page=1', LEN(URL))
Or use CHARINDEX
http://msdn.microsoft.com/en-us/library/aa258228(v=SQL.80).aspx
you can use CHARINDEX in t-sql.
select * from table where CHARINDEX(url, 'http://url.com/url?url...') > 0

How to search multiple columns in MySQL?

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.