I have a table like this:
id path
1 /
2 /city/
3 /city/europe/
4 /city/north america/
6 /city/europe/germany/berlin/
7 /city/europe/germany/
8 /city/north america/usa/
[...]
and a search string like this:
"/city/north america/usa/florida/miami/"
I need to select the longest path thats inside the search string so the result would be
id path
8 /city/north america/usa/
Every parent to any path in the table exists. The paths can be arbitrarily long. The search string contains at least "/".
I can think of a few solutions like using wildcards on the column, splitting the search string and performing a select with IN or joins.
What is the best and fastest way to do this?
The table can be modified in any way.
EDIT:
The fastest solution with which I have come up so far:
SELECT * FROM table_name
WHERE path IN ('/','/city/','/city/north america/', '/city/north america/usa/', '/city/north america/usa/florida/', '/city/north america/usa/florida/miami/')
ORDER by LENGTH(path) DESC LIMIT 1;
SELECT *
FROM table_name
WHERE path REGEXP '^/city((((/north america)?/usa)?/florida)?/miami)?\/?$'
ORDER BY LENGTH(path) DESC
LIMIT 1
Or, try this one:
select * FROM tbl WHERE path=
( select max(path) from tbl
where '/city/north america/usa/florida/miami/' like concat(path,'%')
)
(Edit)
Then maybe this is faster:
select * from tbl where instr('/city/north america/usa/florida/miami/',path)=1
order by path desc limit 1
Related
here is mysql query which displays the entire table.
select * from datas;
name id dept
sen 1 cs
der 2 td
rest 3 ui
My query is i want to search by using table content(i.e in select statement i want to use the word rest and then display the table as follows).How can i do it in mysql?
Expected output:
name id dept
rest 3 ui
From your question, I don't understand why you can't just use the '=' :
SELECT * FROM datas
WHERE name = 'rest'
Although if you're truly looking for a grep equivalent, the LIKE statement is where it's at :
SELECT * FROM datas
WHERE name LIKE '%rest%' --to select lines where rest is contained in the string
Use '=' or LIKE to compare strings in SQL?
There many ways to do this
1.Use the WHERE clause just like this
SELECT * FROM datas WHERE id>2
2.Use LIMIT clause like this
SELECT * FROM datas LIMIT 2,1
I have a mysql query like :
SELECT name FROM table_name WHERE name LIKE '%custom%' limit 10
It retruns me 2 rows from my custom table.
I want to get records which contains either of any word from the text c cu cus cust usto stom tom om m also.
I tried below query :
SELECT name FROM table_name WHERE name like '%custom%' OR name REGEXP 'c|cu|cus|cust|usto|stom|tom|om|m' limit 10
Above query returning me 7 records but these 7 records does not have such 2 records which 1st query result have.
How to get that? Or any other way to get these result in mysql?
EDIT : Here I also want to order by maximum substrings matches in second query.
Try this:
SELECT name FROM table_name WHERE name REGEXP 'custom' limit 10;
There is no need of LIKE with REGEXP, but REGEXP are slower then LIKE. So if your table have so many records then REGEXP quesries are slower.
Try this:
SELECT name FROM table_name WHERE name REGEXP 'custom|c|cu|cus|cust|usto|stom|tom|om|m' limit 10
What we did above is that we combined custom with the rest of the patterns, and we made them all use REGEXP.
You need to add word boundaries, which in MySQL are [[:<:]] for start of word and [[:>:]] for end of word:
SELECT name
FROM table_name
WHERE name REGEXP '[[:<:]](c|cu|cus|cust|usto|stom|tom|om|m)[[:>:]]'
limit 10
See live demo.
Note the brackets around the alternation.
I have a table like:
id name
--------
1 clark_009
2 clark_012
3 johny_002
4 johny_010
I need to get results in this order:
johny_002
clark_009
johny_010
clark_012
Do not ask me what I already tried, I have no idea how to do this.
This will do it, very simply selecting the right-most 3 characters and ordering by that value ascending.
SELECT *
FROM table_name
ORDER BY RIGHT(name, 3) ASC;
It should be added that as your data grows, this will become an inefficient solution. Eventually, you'll probably want to store the numeric appendix in a separate, indexed integer column, so that sorting will be optimally efficient.
you should try this.
SELECT * FROM Table order by SUBSTRING(name, -3);
good luck!
You may apply substring_index function to parse these values -
select * from table order by substring_index(name, '_', -1)
You can use MySQL SUBSTRING() function to sort by substring
Syntax : SUBSTRING(string,position,length)
Example : Sort by last 3 characters of a String
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, -3);
#OR
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, -3,3);
Example : Sort by first 3 characters of a String
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, 1,3);
Note : Positive Position/Index start from Left to Right and Negative Position/Index start from Right to Left of the String.
Here is the details about SUBSTRING() function.
If you want to order by the last three characters (from left to right) with variable name lengths, I propose this:
SELECT *
FROM TABLE
ORDER BY SUBSTRING (name, LEN(name)-2, 3)
The index starts at lenght of name -2 which is the third last character.
I'm a little late but just encountered the same problem and this helped me.
I have a table with a column, lets call it "query", which is a varchar.
I would like to retrieve the values into a paginated list in such a way that each page will contain 208 results, 8 from each letter in the alphabet.
So on page 1 the first 8 results will begin with "a", the next 8 will begin with "b", and so on until "z" (if there aren't any results for that letter then it just continues to the next letter.
On page 2 of the results it would show the next 8 results beginning with "a", the next with "b", and so on.
Basically instead of sorting by query ASC, which will result in the first page having all words beginning with "a", I would like each page to contain words beginning with each letter of the alphabet.
If you feel I did not explain myself properly (I do!), then please feel free to ask. I have the idea in my head but it's not easy translating it into words!
a naive start approach could be:
SELECT * FROM table1 WHERE `query` LIKE 'a%' ORDER BY `query` LIMIT 8
UNION
SELECT * FROM table1 WHERE `query` LIKE 'b%' ORDER BY `query` LIMIT 8
UNION
SELECT * FROM table1 WHERE `query` LIKE 'c%' ORDER BY `query` LIMIT 8
....
The second page would need to be done using
SELECT * FROM table1 WHERE `query` LIKE 'a%' ORDER BY `query` LIMIT 8,8
UNION
....
Third page:
SELECT * FROM table1 WHERE `query` LIKE 'a%' ORDER BY `query` LIMIT 16,8
UNION
....
etc.
I think this is not possible to do in one statement (or, may be possible, but too slow in execution). May be you must take a look on MySQL prepared statements or change behavior of the page.
you can use group by the first letter using
group by left(query, 1)
And from the server side script, show 8 results from the query result (perhaps by using the page page number * 8 and starting from that index for each letter, starting from page 0, that is)
I would never use this query on large dataset but just for personal fun I found this solution:
select *,
ceil(row_num/8) as gr
from (
select
*,
#num := if(#word = substring(word,1,1), #num + 1, 1) as row_num,
#word := substring(word,1,1) as w
from words,(select #num:=0,#word:='') as r order by word ) as t
order by gr,word
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.