We have a search query that allows the users to enter a string and then searches 3 separate fields via LIKE '%string%'. The fields are:
Name
Description
Type
When then return the results and right now just order them arbitrarily. We would prefer the results be ordered first by results found in the Name field, second by items found in the Description field, and then lastly in the Type field. Because we have paging and sorting also associated with this, we really want it returned in one record set. Is this even possible?
Thanks in advance.
In MySQL, the following should work:
SELECT *
FROM atable
WHERE Name LIKE '%string%'
OR Description LIKE '%string%'
OR Type LIKE '%string%'
ORDER BY
CASE
WHEN Name LIKE '%string%' THEN 1
WHEN Description LIKE '%string%' THEN 2
WHEN Type LIKE '%string%' THEN 3
END
;
Please try this :
Select *
from Table
where Name like '%string%'
and description like '%string%'
and type like '%string%'
order by name,description,type
You can try this:
Select *
from Tablename
where Name like '%string%'
and description like '%string%'
and type like '%string%'
order by name,description,type
Related
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%'
i need to find multi string in multi columns in mysql.
select * from myTable WHERE name LIKE '%stack%';
This obviously works. However, what I need to do is that if both name and surname are checked multi string would do something like this:
select * from myTable WHERE (name or surname) LIKE '%stack%','%over%','%flow%';
if similar above code that work does not exist. please tell me how can checking multi string in a column in mySQL with like statement:
select * from myTable WHERE name LIKE '%stack%','%over%','%flow%';
You can keep it simple and do:
select * from myTable WHERE name LIKE '%stack%' OR
name LIKE '%over%' OR
name LIKE '%flow%';
or:
select * from myTable WHERE name LIKE '%stack%' AND
name LIKE '%over%' AND
name LIKE '%flow%';
This doesn't look as nice as the REGEXP from Young, but it doesn't have the unexpected behaviour REGEXP can have given that user input can be anything. With REGEXP you really need to know what you're doing.
You can try:
select * from myTable WHERE name REGEXP 'stack|over|flow';
Though it's not a like solution.
I am building a search function in my "search.php" for a database in which I am storing information on ip-addresses and various units. Each unit has a "category" column in the database. My search choices are either searching in a text field, and/or selecting a category in a drop-down list. When I choose a category in my drop-down list and not typing anything in the text field, I get the correct results (returning all rows containing the selected category).
But when I type anything in the text field, I don't get any results.
Query when I ONLY choose category(in this case "IP-adress" is chosen as category):
SELECT dataid,
firstname,
lastname,
data.name dataname,
description,
ip,
TYPE,
TIMESTAMP
FROM DATA
INNER JOIN USER ON DATA.userid=USER.userid
WHERE TYPE='IP-adress'
AND DATA.name LIKE '%%'
AND description LIKE '%%'
AND ip LIKE '%%'
AND TYPE LIKE '%%'
AND firstname LIKE '%%'
AND lastname LIKE '%%'
ORDER BY TIMESTAMP DESC
This is returning all rows containing the selected category, as I want to.
Query when I type something in the textfield:
SELECT dataid,
firstname,
lastname,
data.name dataname,
description,
ip,
TYPE,
TIMESTAMP
FROM DATA
INNER JOIN USER ON DATA.userid=USER.userid
WHERE TYPE='IP-adress'
AND DATA.name LIKE '%test%'
AND description LIKE '%test%'
AND ip LIKE '%test%'
AND TYPE LIKE '%test%'
AND firstname LIKE '%test%'
AND lastname LIKE '%test%'
ORDER BY TIMESTAMP DESC
I have combinations where I have columns containing the word "test" just as my input, but I get no results.
I have been at it quite some time now and I am quite sure I had the opposite problem before, where I didn't get any results when I chose a category.
Anyway, I hope another set of eyes could help me out here. If you need more code, just ask for it!
This is because you have used AND for all the condition. It will search for test in all fiends and if one field does not have test then condition fails.
Try this way
SELECT dataid,
firstname,
lastname,
data.name dataname,
description,
ip,
TYPE,
TIMESTAMP
FROM DATA
INNER JOIN USER ON DATA.userid=USER.userid
WHERE TYPE='IP-adress'
AND (DATA.name LIKE '%test%'
OR description LIKE '%test%'
OR ip LIKE '%test%'
OR TYPE LIKE '%test%'
OR firstname LIKE '%test%'
OR lastname LIKE '%test%')
ORDER BY TIMESTAMP DESC
If I'm looking at the second query:
WHERE type='IP-adress'
and later on:
AND type LIKE '%test%'
That wouldn't give any result back as 'IP-adress' doesn't contain 'test' in it...
Also, the second query means actually that ALL the fields which are given in the where should contain at least the text 'test'. Are you sure that is correct?
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.
i need some code for search some data inside DB...
i have two fields that is id and name...
if i type "ABC" it can show all data about "ABC"..
if "DEF"=>all DEF data...or i type anything it can show that "anything"...
i'm just know how to show all data inside fields that makes "ABC" and "DEF" show together,the code like this:
"SELECT ID,Name FROM Regist ORDER BY date";
MySQL wildcards: _ AND % (in conjunction with keyword LIKE)
_ denotes a single character
SELECT name FROM colors WHERE name LIKE 'gr_y';
Output: 'grAy', 'grEy'.
% denotes any number of characters
SELECT name FROM colors WHERE name LIKE 'gr%';
Output: 'grAY', 'grEY', 'grEEN'.
You can use both _ and % multiple times
SELECT name FROM color WHERE name LIKE '_ra%';
Output: 'GraY', 'OraNGE'.
In your case, if you want to list the names containing starting with either 'ABC' or 'DEF', your query would be like this:
SELECT id,name FROM Regist
WHERE name LIKE 'ABC%' OR name LIKE 'DEF%'
ORDER BY date
If you want your names to contain the strings 'ABC' OR 'DEF' anywhere inside, your query would be:
SELECT id,name FROM Regist
WHERE name LIKE '%ABC%' OR name LIKE '%DEF%'
ORDER BY date
Finally, if you want your names to contain both 'ABC' AND 'DEF' you could use:
SELECT id,name FROM Regist
WHERE name LIKE '%ABC%DEF%' OR name LIKE '%DEF%ABC%'
ORDER BY date
Probably you want something like this:
SELECT id,name FROM Regist WHERE NAME LIKE '%ABC%' OR NAME LIKE '%DEF%';
Please note that this kind of search is inefficient and if you need something like that you'd better use something like Sphinx
From the little i understand your question
"SELECT ID,Name FROM Regist where name='ABC' ORDER BY date";
is this correct?
"SELECT id,name FROM Regist WHERE id LIKE '%' OR NAME LIKE '%' ORDER BY date";