I am using the OR operator in the following fashion:
SELECT * FROM Customers
WHERE City = "Berlin" OR City = "London"
The above returns a results table containing some data, however what also returns data is:
SELECT * FROM Customers
Where City = "Berlin" OR "London"
I am using a questionable SQL editor currently, and want to know if this would be valid in a MySQL environment.
You are looking for
SELECT * FROM Customers Where City IN('Berlin', 'London');
The query:
Where City='Berlin' OR 'London'
Applies the logical OR operator (||), so OR "London" is equivalent to OR 0, and Where City = 'Berlin' OR 0; will just return 'Berlin'
SqlFiddle here with truth table here
Minor, but you should look at using single quotes for string literals, as this is more portable between RDBMS's and use of " will depend on ANSI QUOTES.
Well, Cassini i have check your syntax and run this query on my table i did not getting same result. I am getting correct output as expected.
i have run this command:
SELECT * FROM `city` where name = 'London' or 'Berlin'
and i got only london in name column. When i run this command:
SELECT * FROM `city` where name = 'London' or name = 'Berlin'
then i got both the cities in name column. so command is valid it will return only valid output which satisfy the condition.
So, i can say that command is valid but execute only that part of query which satisfy MYSQL Select syntax.
Well, if it valid in SQL, it should be valid in MySQL to. Are you wondering if this would be valid in MYSQL
SELECT * FROM Customers Where City="Berlin" OR"London"
It should be valid in MySQL
Hope that helps. If it does not, add a comment
this returnd the same values as your first sql-string.
But your first string is more readable for others.
You can use IN
SELECT *
FROM Customers
Where City IN ("Berlin","London")
Related
I tried to use "||" for concatenation:
The query I used:
"SELECT id, name, age, age||id FROM myTable;"
This is the output:
Can anyone tell me why the output is not 201 (in first column) and 162(in second column)?? Also it gives similar outputs when I use two attributes that are of varchar datatype and the above two attributes are of int datatype.
Can anyone tell me why the output is not 201
its because, in mysql, you need to enable PIPES_AS_CONCAT. in order to work with ||
If the PIPES_AS_CONCAT SQL mode is enabled, || signifies the
SQL-standard string concatenation operator (like CONCAT()).
You can set it using phpmyadmin->variables->searchFor SQL_MODE
Refer mysql doc
But i would suggest you to use
CONCAT(columnName1, columnName2, ...)
There is no such concatenation in mySQL. This is Oracle SQL dialect. You have to use the CONCAT function in mysql
SELECT id, name, age, CONCAT(age,id) FROM myTable
visit the Mysql Documentations :
on this link you will find a list of String Functions and Operators
but you not use|| to concatenate caratcters or strings but do this :
SELECT id, name, age, concat(age,id) FROM myTable; or
SELECT id, name, age, concat_ws(' ',age,id) FROM myTable; if you want to space the age with id like this for example 23 1. 23 for age and 1 for id.
The original question:
Query the list of CITY names from STATION that either do not start with vowels or do not end with vowels. Your result cannot contain duplicates.(source hackerrank)
name of the table: Station; the name of required attribute: city
My query when I use MYSQL:
select distinct city
from station
where lower(city) regexp "^[^aeiou]*"
or lower(city) regexp "[^aeiou]$";
I get names like 'Aguanga' and 'East Irvine' in my output on MySQL BUT when I use oracle and write the same logic in that, I get the correct output. The query I wrote for Oracle:
select distinct city
from station
where regexp_like(lower(city), '^[^aeiou]')
or regexp_like(lower(city),'[^aeiou]$');
I am new at SQL and do not understand why this is not working. I've used this method to get the correct output in MySQL for not starting with a vowel and not ending with a vowel but for 2 different questions separately.
When I run the following on MySql:
SELECT * FROM Customers WHERE City LIKE '[acs]%';
I get the expected results.
However, the same query on mariadb returns an empty set. Am I doing something wrong?
I've looked at the docs and it seems like they want it to be more like
SELECT * FROM Customers WHERE City RLIKE '(a|c|s)'.
Is there a command that will work on both? Should I use REGEXP?
SELECT * FROM Customers WHERE City REGEXP 'a|c|s'
Yo can you the REGEXP function:
SELECT * FROM Customers WHERE City REGEXP '[acs]'
It is highly unlikely that this query returns results on MySQL:
SELECT *
FROM Customers
WHERE City LIKE '[acs]%';
Why? No city names that I know of have the character '['. And, if they did, it seems even less likely that they would have the character ']'.
You may be confusing MySQL with SQL Server. The latter has extended LIKE patterns to include character classes. The equivalent logic in MySQL is:
WHERE City REGEXP '^[acs]'
Or, if you prefer:
WHERE LEFT(City, 1) IN ('a', 'c', 's')
There is table named Students. I want to extract the names of students whose name starts with either 'n' or 'p' or 'y'. I know that in TSQL (MS SQL server) I can write the query as follows and it works:
SELECT * FROM Students WHERE StudentName LIKE '[npy]%'
But when I execute the same query in MySQL (phpmyadmin) I was unable to retrieve the correct result set. I tried converting the letters of the student name into the same case which is mentioned in the charlist. I did bit of googling and found out that in MySQL we need to specify this as a regular expression. I executed the below query and got the expected result.
SELECT * FROM Students WHERE StudentName REGEXP '[[:<:]]n | [[:<:]]p | [[:<:]]y'
I want to know the reason why LIKE '[charlist]%' syntax is not working with MySQL. Is it due to the implementation in MySQL (MySQL doesn't support the syntax) or something wrong with the phpmyadmin version I'm using?
Any help regarding this is highly appreciated. Thanks :)
There is an even shorter way to write your query:
SELECT * FROM Students WHERE StudentName REGEXP '^[npy]'
And if you're concerned about being case sensitive:
SELECT * FROM Students WHERE StudentName REGEXP BINARY '^[npy]'
In MySQL, a REGEXP pattern match succeeds anywhere in the value, which differs from LIKE where the pattern must match the entire value.
The following link will give you a more complete answer:
MySQL Pattern Matching
MySQL :
Case insensitive: SELECT * FROM Students WHERE StudentName RLIKE '^[npy]' ;
Case sensitive : SELECT * FROM Students WHERE StudentName CAST(RLIKE as BINARY) '^[npy]' ;
I try make search for my site. Example: have field name = city, if city is empty my query looks: SELECT * FROM TABLE WHERE CITY = ALL;
I don't know how to write: CITY = ALL correct, expression CITY is not null will be complicated because I should be remove =
Try this:
SELECT *
FROM Table
WHERE #SearchParam IS NULL OR City = #SearchParam
If his parameter #SearchParam is passed to the query with a NULL value then it will return all the data in the table, otherwise it will search for the cities with this parameter.
remove the where completely
SELECT * FROM TABLE
or
SELECT * FROM TABLE WHERE CITY = #searchstring or #searchstring is null;
If you don't care about very optimized queries, you can use a like operator compare with
City like '%#exp'
If exp is empty the query returns all Cities.
I personally do not recommend this :)
All the best
If you are getting them all, why have the WHERE clause in at all?
SELECT * FROM TABLE
Note, you should avoid using SELECT * whenever possible. As it is a waste of resources, always be explicit with your columns returned.
Maybe something like this?
SELECT * FROM TABLE WHERE (CITY = SOMETHING) OR (SOMETHING is null);