A poll result table:
ID, NAME, ANSWER char(1)
query which select Name start with 'N' but answer not have 'Y'
My try
select *
from poll
where name like "N%" AND answer NOT LIKE "Y"
It is not working
You had missing ; after the query. should be used ;
select *
from poll
where name like 'N%' AND answer NOT LIKE 'Y';
Related
I made table copied from W3schools 2015 ... Here's some of the data:
here the columns and data types i used as follows.
CustomerID = int
CustomerName = varchar
ContactName = varchar
Address = varchar
City = varchar
PostalCode = varchar
Country = text
When i used the following query i got the actual result.
SELECT * FROM Customers WHERE Country LIKE 'U%'; SELECT * FROM Customers WHERE Country LIKE 'M%'; SELECT * FROM Customers WHERE Country LIKE 'G%';
Now the problem was that When i used the following query, i weren't getting actual result.
SELECT * FROM Customers WHERE Country LIKE 's%';
i didn't see any row !!
But We should have seen the following output,
why ?? can anybody explain pls...
Moreover I am totally in Novice phase..
Thanks in Advance
May be,
This is Because of Your Country Column Starting with 'S'(which we think) are not really Starting with 'S', May be it's first charter is a Space.(Just A Guess)
so, For this Case you can try Once,
SELECT * FROM Customers WHERE rtrim(ltrim(Country)) LIKE 's%';
or
SELECT * FROM Customers WHERE replace(Country,' ','') LIKE 's%';
See How can I search (case-insensitive) in a column using LIKE wildcard?
I m trying to query a database with about 2000 entries. I want to select the entries in which the names may contain any one of the vowel.
I tried using the following query, but it gives me those entries that contain all the given characters in that order.
select * from myTable where name like '%a%e%i%';
How do I modify the above query to select those entries with names that may contain at least anyone of the vowels.
Try this for SQL Server:
SELECT * FROM myTable WHERE name LIKE '%[AEIOU]%';
I hope this helps you...
SELECT * FROM myTable WHERE name REGEXP 'a|e';
or.....
SELECT * FROM myTable WHERE name REGEXP 'a|e|i';
In SQL Server, you would do:
where name like '%[aeiou]%';
In MySQL, you would do something similar with a regular expression.
Use OR like this.
This will work for both SQL Server and MySql.
select * from myTable where name like '%a%' OR name like '%e%' OR name like '%i%';
Use LIKE and OR.
Query
select * from myTable
where name like '%a%'
or name like '%e%'
or name like '%i%'
or name like '%o%'
or name like '%u%'
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.
I am trying to write an mysql script to see if the first character is a number or not, this is what I go so far
select CASE WHEN user_id REGEXP '[0-9]+' then 1 else 0 end as user_id from table
it returns all my data as 1...the colum is a varchar, I will have user_id like this 1234 or this USER-484 or `ADMIN-464567' IS what I am trying to do possible?
So you want to display the user_id instead? Then put your regexp in the WHERE clause
select user_id from table
where
user_id REGEXP '^[0-9]+'
Also you're missing a ^ which means "at the beginning of the line"
Try this to check if starting letter/digit is a number:
select CASE WHEN user_id
REGEXP '^[0-9]+'
then 1 else 0 end as user_id
from table;
or
select CASE WHEN user_id
REGEXP '^\d+'
then 1 else 0 end as user_id
from table;
I am just wondering about your query arrangement. I thought it should be like this,
select user_id
from table
where REGEXP '^\d+' ;
But yours is actually working with the correct regex. :)
* SQLFIDDEL DEMO
Since you are missing ^ before [0-9] it matches numbers at any place in your field. By specifying ^[0-9] it means that it should BEGIN with a number. So try using '^[0-9]'
i am trying to sort mysql data with alphabeticaly like
A | B | C | D
when i click on B this query runs
select name from user order by 'b'
but result showing all records starting with a or c or d i want to show records only starting with b
thanks for help
i want to show records only starting with b
select name from user where name LIKE 'b%';
i am trying to sort MySQL data alphabeticaly
select name from user ORDER BY name;
i am trying to sort MySQL data in reverse alphabetic order
select name from user ORDER BY name desc;
but result showing all records
starting with a or c or d i want to
show records only starting with b
You should use WHERE in that case:
select name from user where name = 'b' order by name
If you want to allow regex, you can use the LIKE operator there too if you want. Example:
select name from user where name like 'b%' order by name
That will select records starting with b. Following query on the other hand will select all rows where b is found anywhere in the column:
select name from user where name like '%b%' order by name
You can use:
SELECT name FROM user WHERE name like 'b%' ORDER BY name
If you want to restrict the rows that are returned by a query, you need to use a WHERE clause, rather than an ORDER BY clause. Try
select name from user where name like 'b%'
You do not need to user where clause while ordering the data alphabetically.
here is my code
SELECT * FROM tbl_name ORDER BY field_name
that's it.
It return the data in alphabetical order ie; From A to Z.
:)
I had the same challenge, but after little research I came up with this and it gave me what I wanted, and I was able to overcome that path.
SELECT * from TABLE ORDER BY name
Wildcard Characters are used with like clause to sort records.
If we want to search a string which is starts with B then code is like the following:
select * from tablename where colname like 'B%' order by columnname ;
If we want to search a string which is ends with B then code is like the following:
select * from tablename where colname like '%B' order by columnname ;
If we want to search a string which is contains B then code is like the following:
select * from tablename where colname like '%B%' order by columnname ;
If we want to search a string in which second character is B then code is like the following:
select * from tablename where colname like '_B%' order by columnname ;
If we want to search a string in which third character is B then code is like the following:
select * from tablename where colname like '__B%' order by columnname ;
Note : one underscore for one character.
I try to sort data with query it working fine for me please try this:
select name from user order by name asc
Also try below query for search record by alphabetically
SELECT name FROM `user` WHERE `name` LIKE 'b%'
MySQL solution:
select Name from Employee order by Name ;
Order by will order the names from a to z.