Mysql REGEXP 'o' OR 'aw' - mysql

I'm trying to do a friend search app.
So to do that I'm using regular expressions like:
SELECT ...
WHERE firstname REGEXP $firstname
And the $firstname variable is a string - for example:
(ch|k)ris
But for some reason it does not work. maybe the ( | ) doesn't work in MySQL?
What can I use instead of that operator to do the same thing?

in PHP modify your line to this:
$query = "SELECT ... FROM users WHERE id NOT IN($alreadyfriend) AND firstname REGEXP '$firstname'"; //notice the single quotes

Related

How to get multiple value in one variable with ' '

How i can get value like this in a variable 'TFSEP-2019','TFjul-2018','TFJun-2018' without spaces.
SELECT s.house, s.grade, s.homeroom AS Campus
FROM student s,fees_jrl f
WHERE s.studnum = f.studnum AND
f.name IN (' TFJun-2018 TFJul-2018 ') AND
f.trans_type= 'chg' AND
f.paid_id is NULL AND s.house LIKE '%'
GROUP BY s.house
I am getting like this ('TFJun-2018 TFJUL-2018 TFSEP-2019') but I want like ('TFSEP-2019','TFjul-2018','TFJun-2018') please help
You may refer the following statements with like operators, whenever you need quotes('), use doubled-quotes('') :
create table tab ( Id int, expression varchar(100));
insert into tab values(1 ,'TFJun-2018 TFJUL-2018 TFSEP-2019');
insert into tab values(2,'''TFJun-2018'',''TFJUL-2018'',''TFSEP-2019''');
select * from tab where expression like '''TFJun-2018''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
or
select * from tab where expression like '''TFJun-2018'',''TFJUL-2018''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
or
select * from tab where expression like '''TFJun-2018'',''TFJUL-2018'',''TFSEP-2019''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
Rextester Demo
You can use like for strings
% means any chars, _ means one char
So doing
F.name like %2018%
Will give you all of 2018..
use explicit join and concat
SELECT
concat( concat( concat( concat("'",s.house),"'"),concat( concat("'",s.grade),"'")),
concat( concat("'",s.homeroom),"'"))
FROM student s join fees_jrl f
on s.studnum = f.studnum
where f.name IN (' TFJun-2018 TFJul-2018 ') AND
f.trans_type= 'chg' AND
f.paid_id is NULL AND s.house LIKE '%'
I think you dont need group by as you have no aggregation function

Search special character from SQL server 2008?

I am searching a specific record from my data base Like "Case Name" and Case name field includes special characters also Like "Case-1" , "Case_1" , "Diana's Case" , etc.
So when I search for specific case name using query
select * from TableName where CaseName = 'Case_1'
Then I can find it. But problem is here that if I want to search
select * from TableName where CaseName = 'Diana's Case'
Then it throws exception because of " ' " character.
Is there any solution for it?
Use double ' inside string to escape it.
select *
from TableName
where CaseName = 'Diana''s Case'
Demo
Try this,
SELECT *
FROM TableName
WHERE CaseName = 'Diana''s Case'

Rails & MySQL - How do I use the LIKE operator with % in find_by_sql in rails?

How do I remove the quotation marks around a variable so that I can use the LIKE operator in find_by_sql in rails?
#entries1 = Entry.find_by_sql(["SELECT `entries`.name as name FROM `entries` where `entries`.name like '%?%'",#something])
will yield
SELECT `entries`.name as name FROM `entries` where `entries`.name like '%'hello'%'
when #something = 'hello'
Instead of putting the percent symbols in the SQL, add them to the variable you're injecting (after escaping percent signs/underscores already present!)
"%#{#something.gsub('%', '\%').gsub('_', '\_')}%"
You could use concat inside the database:
Entry.find_by_sql([
"SELECT ... `entries`.name like concat('%', ?, '%')",
#something
])
or you can add the wildcards in Ruby:
Entry.find_by_sql([
"SELECT ... `entries`.name like ?",
"%#{#something}"
])
Note that other databases will want to see '%' || ? || '%' or '%' + ? + '%' instead of the concat call.
#entries1 = Entry.select('name').where('name like ?',"%#{#something}%"]) should do the same
You need four '%' around the search value and must precede each with delimiter ‘\’.
For example search_val can be “” or “031”, etc...
find_by_sql([ "select * from tableX where id like '\%\%#{search_val}\%\%'" ])

How to replace only few characters from a string in mysql?

I have a field urn_sem.studentid that I'd like to replace a few characters in; for example:
ABC/2011/BCOMH_NC/I/12 → ABC/2011/BCOMH/I/12
ABC/2011/BCOMH_NC/I/24 → ABC/2011/BCOMH/I/24
I've tried this query:
SELECT REPLACE(studentid, 'KNC/2011/BCOMH_NC/', ' KNC/2011/BCOMH/')
FROM urn_sem
but it doesn't show the new value.
Do you want this:
update urn_sem
set studentid = REPLACE(studentid, 'KNC/2011/BCOMH_NC/', ' KNC/2011/BCOMH/')
where studentid like '%KNC/2011/BCOMH_NC/%'
The WHERE clause is optional. It ensures that the replace is only on rows that change.
And this sample query does not work?
SELECT REPLACE (studentid, '_', '') FROM urn_sem

MySQL, Concatenate two columns

There are two columns in a MySQL table: SUBJECT and YEAR.
I want to generate an alphanumeric unique number which holds the concatenated data from SUBJECT and YEAR.
How can I do this? Is it possible to use a simple operator like +?
You can use the CONCAT function like this:
SELECT CONCAT(`SUBJECT`, ' ', `YEAR`) FROM `table`
Update:
To get that result you can try this:
SET #rn := 0;
SELECT CONCAT(`SUBJECT`,'-',`YEAR`,'-',LPAD(#rn := #rn+1,3,'0'))
FROM `table`
You can use mysql built in CONCAT() for this.
SELECT CONCAT(`name`, ' ', `email`) as password_email FROM `table`;
change field name as your requirement
then the result is
and if you want to concat same field using other field which same then
SELECT filed1 as category,filed2 as item, GROUP_CONCAT(CAST(filed2 as CHAR)) as item_name FROM `table` group by filed1
then this is output
In php, we have two option to concatenate table columns.
First Option using Query
In query, CONCAT keyword used to concatenate two columns
SELECT CONCAT(`SUBJECT`,'_', `YEAR`) AS subject_year FROM `table_name`;
Second Option using symbol ( . )
After fetch the data from database table, assign the values to variable, then using ( . ) Symbol and concatenate the values
$subject = $row['SUBJECT'];
$year = $row['YEAR'];
$subject_year = $subject . "_" . $year;
Instead of underscore( _ ) , we will use the spaces, comma, letters,numbers..etc
In query, CONCAT_WS() function.
This function not only add multiple string values and makes them a single string value. It also let you define separator ( ” “, ” , “, ” – “,” _ “, etc.).
Syntax –
CONCAT_WS( SEPERATOR, column1, column2, ... )
Example
SELECT
topic,
CONCAT_WS( " ", subject, year ) AS subject_year
FROM table
I have two columns:
prenom and nom so to concatenate into a column with name chauffeur_sortant I used this script:
SELECT date as depart, retour, duree_mission, duree_utilisation, difference, observation, concat( tb_chaufeur_sortant.prenom, ' ', tb_chaufeur_sortant.nom) as chauffeur_sortant, concat(tb_chaufeur_entrant.prenom, ' ', tb_chaufeur_entrant.nom) as chauffeur_entrant
FROM tb_passation
INNER JOIN tb_vehicule
ON tb_vehicule.id = tb_passation.id_vehicule
INNER JOIN tb_chaufeur_sortant
ON tb_chaufeur_sortant.id = tb_passation.id_sortant
INNER JOIN tb_chaufeur_entrant
ON tb_chaufeur_entrant.id = tb_passation.id_entrant WHERE tb_vehicule.id = '';
$crud->set_relation('id','students','{first_name} {last_name}');
$crud->display_as('student_id','Students Name');