mysql order by, showing uppercase results first - mysql

I have a simple mysql query which fetches AES_ENCRYPTED names from the table,
I want the result to show in alphabetical order,, but the problem is the result is showing capital results first..
I mean like
A
B
a
b
but I want like
A
a
B
b
this is my query..
select
id as id,
AES_DECRYPT(fname,'$_ENCKEY') as fname1,
AES_DECRYPT(lname,'$_ENCKEY') as lname
from patient
order by fname1 ASC
I also have tried LOWER, UPPER, UCASE etc.... but no luck...
And I am using PDO...

Try to cast the return value of AES_ENCRYPT to CHAR.
select
id as id,
CAST(AES_DECRYPT(fname,'$_ENCKEY') AS CHAR CHARACTER SET latin1 ) as fname1,
AES_DECRYPT(lname,'$_ENCKEY') as lname
from patient
order by fname1 collate latin1_general_cs

It's a case sensitive problem :
Try :
select
id as id,
AES_DECRYPT(fname,'$_ENCKEY') as fname1,
AES_DECRYPT(lname,'$_ENCKEY') as lname
from patient
order by fname1 COLLATE 'latin1_swedish_ci' ASC

Related

mysql query to select user count with Like clause

I have a forum, there's an option for user to change their names. Also i have staff members with names like [ADM]James. I want to prevent users to change his name in James.
Example: I have a memeber from staff with name [ADM]James, users should not be able to change his name in simply James.
There's my query : SELECT COUNT(*) FROM users WHERE name='James' LIKE '[%%' However this query return strange numbers it always return whole count of users.
What i do wrong?
EDIT, WHAT IS UNCLEAR?
I have 2 staff members. Admins and moderators, admins have tag name [ADM] moderators [MOD]. Example two names: [ADM]James [MOD]Jeff .. When a user try to change his name in James count should return 1. Because there is already one staff memeber with name [ADM]James.
CREATE TABLE `user` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`user` varchar(24) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'NONAME',
) ENGINE = MyISAM AUTO_INCREMENT = 191782 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
Use below query,
SELECT COUNT(*) FROM users WHERE name LIKE '%]%'
SELECT user, COUNT(*) FROM users WHERE name LIKE '%[ADM]%' group by user;
SELECT COUNT(*) FROM users WHERE name LIKE '%James%'
Will count every name that has 'James' on it.
WHERE name='James' LIKE '[%%' is not a valid where clause
The syntax of the LIKE operator is expr LIKE pat [ESCAPE 'escape_char']
You can either use % or _
% matches any number of characters, even zero characters.
_ matches exactly one character.

UNION three different SELECTs in MYSQL

I have this MYSQL table named people with the columns: id|firstname|lastname|birthdate|phone.
I am quite new to MYSQL and I'm trying to UNION several SELECTs so that the result will look in the following way:
only the first 20 results must be shown
the first SELECT criteria is by the combination firstname+lastname+birthdate: WHERE (birthdate="1980-01-01") AND ((firstname LIKE "%john%") AND (lastname LIKE "%smith%"))
the second SELECT criteria is by the combination firstname+lastname: WHERE (firstname LIKE "%john%") AND (lastname LIKE "%smith%")
the third SELECT criteria is by phone: WHERE phone="0123456"
the output result must in fact have 3 columns: order|id|type; where "order" and "type" are alias columns
the exported ids must be unique: if the same id results from all the 3 SELECTs (or from more than one SELECT), then it must appear only once in the output table
the column "order" must have the value 1 for the results of the first SELECT, 2 for the 2nd SELECT and 3 for the last SELECT
if the same id value results from more than one SELECT, then its row must have the highest order value; where the highest order posible is 1, from the first SELECT
the alias column "type" must work like this: if an id results from the 1st SELECT, it's type value is "~firstname+lastname+birthdate~"; if an id results from the 2nd SELECT, it's type value is "~firstname+lastname~"; and finally if an id results from the 3rd SELECT, it's type value is "~phone~"
if the same id value results from more than one SELECT, the value on the "type" alias column must be a concatention between the SELECTs where that id was found (for example, if the same id resulted in all 3 SELECT queries then the value on the "type" column would be "~firstname+lastname+birthdate~~firstname+lastname~~phone~")
Is it possible to achieve such an output?
Here's something using CASE statements. I think you'll get into a mess with union statements, because of your order type statement. Hopefully I've understood what you're after - it's much easier if you post sample data! Anyway, even if this doesn't do exactly what you want, you get the idea....
[EDIT] I don't think you need the distinct, but I don't think it hurts, either...
SELECT DISTINCT
CASE WHEN birthdate='1980-01-01'
AND firstname LIKE '%john%' AND lastname LIKE '%smith%'
THEN 1
WHEN firstname LIKE '%john%' AND lastname LIKE '%smith%'
THEN 2
WHEN phone='0123456'
THEN 3
END AS outputorder, -- avoid confusion of using an SQL keyword as a column name,
id,
CONCAT(
CASE
WHEN birthdate='1980-01-01'
AND firstname LIKE '%john%' AND lastname LIKE '%smith%'
THEN CONCAT('~',firstname,'+',lastname,'+','~')
END ,
CASE WHEN firstname LIKE '%john%' AND lastname LIKE '%smith%'
THEN CONCAT('~',firstname,'+',lastname,'~')
END ,
CASE WHEN phone='0123456'
THEN CONCAT('~',phone,'~')
END
) -- end the concat
AS outputtype
FROM
mytable
WHERE
( birthdate='1980-01-01'
AND firstname LIKE '%john%' AND lastname LIKE '%smith%')
OR
(firstname LIKE '%john%' AND lastname LIKE '%smith%')
OR
phone='0123456'
ORDER by 1,2 LIMIT 20
In the end I did something like this and it worked just fine:
SELECT MIN(`ord`) AS `order` , `id` , GROUP_CONCAT(`spec`) as `type` FROM (
SELECT "1" AS `ord` , `id` , "~firstname+lastname+birthdate~" AS `spec` FROM `people` WHERE (`birthdate` = "1986-04-02") AND (`lastname` LIKE "%smith%") AND (`firstname` LIKE "%john%")
UNION
SELECT "2" AS `ord` , `id` , "~firstname+lastname~" AS `spec` FROM `people` WHERE (`lastname` LIKE "%smith%") AND (`firstname` LIKE "%john%")
UNION
SELECT "3" AS `ord` , `id` , "~phone~" AS `spec` FROM `people` WHERE (`phone`="0123456")
) final
GROUP BY final.`id`
ORDER BY `order` ASC
LIMIT 20
Thanks to mlinth for the alternative though...

Mysql Select to columns as one but display only one where value %like%

I am trying to search two columns where value is alike and only display the data of the column that has the value not the other column, both columns can not contain the same value.
My code
SELECT CONCAT(`fname`,`lname`)as name ,date
FROM `table` WHERE id='1' AND CONCAT(`fname`,`lname`) LIKE '%james%'
ORDER BY date ASC LIMIT 0,1
The current query outputs both fname and last name value where it finds the %like% i just want it to output only the column where the %like% is found.
Try this:
SELECT
CASE
WHEN fname LIKE '%james%'
THEN fname
ELSE lname
END
AS name
FROM `table`
WHERE id='1' AND CONCAT(`fname`,`lname`) LIKE '%james%'
ORDER BY date ASC LIMIT 0,1
select concat(if(fname, fname, ""), if(lname, lname, "")) as name, date ...

Select row by using one distinct column

I have one table named dictionarydefinition.
CREATE TABLE dictionarydefinition (
id bigint NOT NULL,
definition character varying(1024) NOT NULL,
word character varying(200) NOT NULL,
grammertypename character varying(20) NOT NULL,
)
I have sql command Select * from dictionarydefinition where word like 'someword%'.
Results are multiple rows that got same value. For example if someword% is just empty ''
the result will be:
A
A
A
B
B
C
D
D
D
I just want result be:
A
B
C
D
I have used GROUP BY command, but it takes too much time to process 30MB database for my android device.
What kind of SQL commands I can add to make it choose only one row which got someword% value?
For the example you have mentioned, you can use GROUP BY.. Suppose the column-name for the alphabets is word, the command would be :
SELECT * from dictionarydefinition where word like 'someword%' GROUP BY word;
You can use SELECT TOP or LIMIT or ROWNUM
SELECT TOP 1 * from dictionarydefinition where word like 'someword%';
or
SELECT * from dictionarydefinition where word like 'someword%' LIMIT 1;
or
SELECT * from dictionarydefinition where word like 'someword%' AND ROWNUM <= 3

mysql alphabetical order

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.