MySQL SELECT "LIKE" parameter not working properly - mysql

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?

Related

ORDER BY soundex with WHERE (MySql)

Query code:
SELECT *
FROM example
WHERE name LIKE '%test%'
OR SOUNDEX(name) LIKE 'T230%'
OR SOUNDEX(name) LIKE 'T23%'
I want to show first the results matched with WHERE name LIKE '%test%' and after SOUNDEX(name) LIKE 'T230%' and the lasts rows is the result of SOUNDEX(name) LIKE 'T23%'
Thank you for the attention.
You can use boolean expressions in order by. The "true" is treated as "1" and false as "0". So:
ORDER BY (name LIKE '%test%') DESC,
(SOUNDEX(name) LIKE 'T23%') DESC,
(SOUNDEX(name) LIKE 'T230%') DESC

Ordering SQL results by the field where they are found

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

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.

Grouping multiple MySql queries

I have a simple query as listed below
SELECT id, name, email FROM users WHERE group_id = 1
This works great until, I then start adding LIKE queries, chained with OR statements to the end.
SELECT id, name, email FROM users
WHERE group_id = 1
AND id LIKE $searchterm
OR name LIKE $searchterm
OR email LIKE $searchterm
Suddenly my WHERE clause is no longer upheld and results with a 'group_id' of 2 or 3 are retrieved.
Is there a way I can group WHERE clauses so that they are always upheld or am I missing something obvious?
Dealing with the query first - you need to use brackets for the WHERE clause to be interpreted correctly:
SELECT id, name, email
FROM users
WHERE group_id = 1
AND ( id LIKE $searchterm
OR name LIKE $searchterm
OR email LIKE $searchterm)
I'd be looking at using Full Text Search (FTS) instead, so you could use:
SELECT id, name, email
FROM users
WHERE group_id = 1
AND MATCH(id, name, email) AGAINST ($searchterm)
Mind that the USERS table needs to be MyISAM...
I assume you want
email FROM users WHERE group_id = 1 AND (id LIKE $searchterm OR name LIKE $searchterm OR email LIKE $searchterm)
Here is the mysql operator precedence table

mysql: need code for search data

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";