BETWEEN IN in SQL - mysql

I have my_own table with columns - PersonID, FirstName, LastName, City, age and WorkStatus. Write BETWEEN IN syntax with WHERE condition, it doesn't return any rows but doesn't give any error.
SELECT PersonID,FirstName,LastName,City,age,WorkStatus
FROM sakila.my_own
WHERE (FirstName BETWEEN 'Omer' AND 'Amrah')
AND NOT WorkStatus IN ('Unemployee');
Why does it do that? If any one has any answer, please share with me.
Thanks in advance for answers.

you query:
SELECT PersonID,FirstName,LastName,City,age,WorkStatus
FROM sakila.my_own
WHERE (FirstName BETWEEN 'Omer' AND 'Amrah')
is syntactically correct but not semantically correct. More specifically the clause: FirstName BETWEEN 'Omer' AND 'Amrah' will always result in an empty set, because 'Omer' > 'Amrah', string wise. Try:
FirstName BETWEEN 'Amrah' AND 'Omer'

You should change that BETWEEN condition to using LIKE operator like
WHERE FirstName LIKE '%Omer%' Or FirstName LIKE '%Amrah%'
AND WorkStatus <> 'Unemployee';

You are comparing string using BETWEEN.
So obviously it will did not compare it as numeric or date field.
And one more thing
AND NOT WorkStatus IN ('Unemployee');
Instead of this you have to put
AND WorkStatus NOT IN ('Unemployee');

Related

MySQL match a value with wildcard in multiple columns

I have a teachers table that looks like this:
teacherid
teacherfname
teacherlname
salary
1
Alexander
Bennett
55.30
I would like to return any record that contains a given string in the teacherfname, teacherlname and salary columns.
What I have right now (this returns exact match only):
SELECT * FROM `teachers` WHERE 'Alexander' IN (teacherfname, teacherlname, salary)
What I would like to do is something like this (this would not return anything):
SELECT * FROM `teachers` WHERE '%Alex%' IN (teacherfname, teacherlname, salary)
What do I need to make the query work? Thank you.
I would assume that the value %Alex% won't ever match the salary column. If you want to search for any rows where the first name or last name include "Alex" I would use simple pattern matching, and force all comparisons to use the same letter case.
For example:
SELECT *
FROM `teachers`
WHERE lower(teacherfname) like '%alex%'
or lower(teacherlname) like '%alex%'

Replace function in mysql not working

I have a table like this;
Lastname
MORALES
THOMPSON
SMITH
but I want to use the replace function to change all the lastname to another character like this:
I want 'MORALES' to be replace with 'TEYE'
I tried this syntax;
select lastname, REPLACE(lastname, 'M', 'TEYE')
from customers;
but this is what i'm getting;
'MORALES TEYEORALES'
Instead of
'MORALES TEYE'
please I need help.
Thanks
You told it only to replace M, not the whole name, that's what it did. If you want to replace the whole name, write:
SELECT lastname, REPLACE(lastname, 'MORALES', 'TEYE')
FROM customers
REPLACE() replaces substrings, so if there's a lastname = AMORALES, the result will be ATEYES. If you only want to replace it when it's the entire name, you can use:
SELECT lastname, IF(lastname = 'MORALES', 'TEYE', lastname)
FROM customers
not an expert in mySQL; but, it looks like you are getting what you asked for, the last name folowed by the last name where the "M" was replaced with "TEYE".
have you tried appending the string you want added?
select lastname + " TEYE" (or some variation to concatenate in mySQL syntax.
if the last name only needs to be update if it currently equals "MORALES", you can most likely add a where (row selection) clause (something like where lastName = "morales") to filter the rows being updated

Combine CONCAT and RTRIM in WHERE SQL

I'm in a bit of trouble at the moment and have no idea how to fix this.
Some background info first:
I have a table with 71k records, with columns firstname and lastname. A client wants to be able to search with firstname and lastname combined.
I used this query and it works fine:
select *
from `subscribers`
where `subscribers`.`deleted_at` is null
and concat (firstname," ",lastname) = 'firstname lastname'
except for the following problem:
some records have a trailing whitespace behind their firstname. which makes the above query not work, unless I add a second space in between firstname and lastname.
I know I have to use RTRIM on my firstname, which I tried, but doesn't seem to work.
What am I doing wrong? How can I fix this?
I rather don't edit 71k records so they don't have a trailing space behind their names anymore...
Appreciate your help!
This should work (see SQL Fiddle demo):
select * from `subscribers`
where `subscribers`.`deleted_at` is null
and concat (trim(firstname)," ",trim(lastname)) LIKE 'firstname lastname';

Sql query returns only one row (LIKE)

SELECT *
FROM customers
WHERE Firstname LIKE 'George'
The problem is that i have more than 1 rows in the table with tha name Geoge and the result of the query shows only one row
You will want to include the wildcard % character to include the rows the have George present in the name:
SELECT *
FROM customers
WHERE Firstname LIKE '%George%';
If George will always appear at the beginning, then you can include the wildcard on the end:
SELECT *
FROM customers
WHERE Firstname LIKE 'George%';
you need to add a wildcard character % to match any value that contains george
SELECT *
FROM customers
WHERE Firstname LIKE '%George%'
MySQL LIKE Operator
the statement
WHERE Firstname LIKE 'George'
is equivalent with
WHERE Firstname = 'George'
that is why you are only getting one record which firstname is george.
UPDATE 1
SQLFiddle Demo
try
LOWER(Firstname) LIKE '%george%'
handles partial values and avoids case sensietivity issues.

SQL query to search in a concated string

In a database i have a table prospect and has two columns firstname and lastname.
Now the issue is that i want to search in both columns; the easy solution would be to use a query like
SELECT * FROM `prospect` WHERE lastname like '%piece of lastname%' or firstname like '%piece of firstname%'
This however requires to have two search fields, firstname and lastname. I want that users can search in one field. How should a query look like when I want to achieve this?
Do you mean you want to search the concatenation of two fields? Then you can use something like:
SELECT * FROM prospect
WHERE CONCAT(firstname,' ',lastname) LIKE '%ohn Smit%'
Is this is what you are looking for?
SELECT * FROM prospect
WHERE firstname + ' '+ lastname LIKE '%name%'