I do have a FIELD called CUSTOMERID in my Database which has values that look like this:
SFFG2443
The last four characters are always a four digit number which counts up, the first four characters are some random letters means it looks like that:
SFFG2443
FGTG2444
XDGG2445
...
I simply want to sort my table by only taking the LAST four digits(2443,2444,2445) into account. Is that possible with a single SQL-Statement? Thank you very much in advance.
SELECT * FROM yourtable
ORDER BY RIGHT(CUSTOMERID,4)
Should do the trick!
The Comment from Nobert van Nobelen works perfectly. The whole query looks like this:
SELECT customerid FROM customers ORDER BY substr(customerid,4,8) DESC LIMIT 0,1
Here is a link to the documentary for substr().
Related
I'm trying to modify an existing script I have, that will only output rows that contain specific area codes to users that have be assigned the specific area codes.
Example:
A column named designed_areas in the users table will contain different data like: CM,SS,RH
When a lead comes in, I'm using substr to detect the first 2 characters of a post code.
$trimmed = substr("$postcode", 0, 2);
$viewLeads=mysqli_query($con,"SELECT * FROM leads WHERE team = '$getID->team' ORDER BY id DESC");
while($lead=mysqli_fetch_object($viewLeads)){
I'd like to expand on the mysqli_query and select all the leads from the database where the first 2 characters of the postcode matches any of users designated_areas.. which are comma seperated.
This way, he'll only see leads with the areas he can work on.
Can anyone help?
You can try to use a query like:
SELECT * FROM leads WHERE team = '$getID->team' and designed like '%{$trimmed}%' ORDER BY id DESC;
Php code:
$viewLeads=mysqli_query($con,"SELECT * FROM leads WHERE team = '$getID->team' and designed like '%{$trimmed}%' ORDER BY id DESC;");
You can do it with a regex. For example, in MySQL, if your postal_code table looks something like this:
PersonID Zips
1 11111,22222,33333
2 22222,12121,32323
And you want persons that have postal codes like 33*
SELECT PersonID FROM `postal_code` WHERE zips rlike '33[0-9]{3}'
This assumes that all postal codes are 5 digits and you're always searching using 2 digits. It basically says "find me 5 digit numbers where the first two digits are 33" or whatever your search numbers are.
The PHP code might look like:
$search = 33 ;
$sql = "SELECT PersonID FROM `postal_code` WHERE zips rlike '$search[0-9]{3}'" ;
This solution will find matches regardless of commas, spaces, etc. You won't need any further processing in your PHP code.
In a case where I have a column that needs to be unique eg. product:eggs, tomatoes, pepper, pepper1, pepper2
and before i insert another pepper i need to check the last integer, and add 1 to it, so the next pepper would be 'pepper3'
How would i do this?
Thanks in advance
The easy way is to have two columns: the first for the label and the second for the id.
It's never good to mix up various information in the same column.
Then you could do something like :
SELECT MAX(product_id) FROM ... WHERE label = "pepper"
and
SELECT CONCAT(label,product_id) FROM ... WHERE id = ...
Returns what you want.
I would try something like this:
SELECT LEFT(product_name, 6) AS product_name_plain,
CAST(RIGHT(product_name, 6) AS UNSIGNED) AS product_number
FROM product_table
WHERE product_name_plain = "pepper"
ORDER BY product_number DESC
LIMIT 1
The SELECT breaks apart product names into the plain name ("pepper"), and an unsigned integer version of the product number (3).
The WHERE clause identifies the peppers.
The ORDER BY will sort them (which should result in the last pepper being the first result)
The LIMIT will only fetch the one result.
Note that "6" and "pepper" are hard-coded in this query, your code would have to put them in. 6 is the length of "pepper."
What should I use, to search for a keyword with mysql ?
I have a word and in the query I have
wordless
something word something
someword some
else
other
wordother
thingword
I want to output everything that has the word inside it, but the output to be like first outputed rows to be that rows with word as first letter on them, for example
wordless - will be the first because word are first characters of the word wordless
and the wordother to be outputed to to the first outputed rows, then after them to output something word something and etc, every word that contains the name word, but again to output first that rows that have the word at the first characters.
EDIT:
SELECT *,MATCH(add_songName) AGAINST('d' IN BOOLEAN MODE) asscoreFROM songs WHERE MATCH(add_songName) AGAINST('d') ORDER BYscoreDESC , Here i'm searching for d but it gives me an error -
Can't find FULLTEXT index matching the column list SELECT *,MATCH(add_songName) AGAINST('d' IN BOOLEAN MODE) as `score` FROM songs WHERE MATCH(add_songName) AGAINST('d') ORDER BY `score` DESC
Try to use Levenshtein algorithm in MySQL.
Levenshtein matching is a metric for measuring the amount of difference between two sequence, here it is strings. By default MySQL does not have this function, but you can write and add one.
Please take a look at the code here and add that code as a system function in MySQL, please see the example below on how to get the similarity of two strings.
Please see: https://github.com/rakesh-sankar/Tools/blob/master/MySQL/Levenshtein.txt
Example:
SELECT column1, LEVENSHTEIN(column1, 'matchme') AS perfectmatch FROM sometable ORDER BY perfectmatch DESC
I'm developing a search function for a website. I have a table called keywords with two fields id and keyword. I have two separate search queries for AND and OR. The problem is with the AND query. It is not returning the result that I expect.
The printed SQL is :
SELECT COUNT(DISTINCT tg_id)
FROM tg_keywords
WHERE tg_keyword='keyword_1'
AND tg_keyword='keyword_2'
The count returned is 0, while if I perform the same SQL with OR instead of AND the count returned is 1. I expected the count to be 1 in both cases, and I need it to be this way as the AND results will take priority over the OR results.
Any advice will be much appreciated.
Thanks
Archie
It will always return 0, unless keyword_1=keyword_2. tg_keyword can only have one value, and when you say AND, you're asking for both conditions to be true.
It's the same, logically speaking, as asking "How many friends do I have whose name is 'JACK' and 'JILL'"? None, nobody is called both JACK and JILL.
I don't know what your table looks like and how things are related to each other, but this query makes no sense. You're returning rows where the keyword is one thing and another thing at the same time? That's impossible.
You probably have another table that links to the keywords? You should search with that, using a join, and search for both keywords. We could give you a more precise answer if you could tell us what your tables look like.
EDIT: Based on what you wrote in a comment below (please edit your question!!), you're probably looking for this:
SELECT COUNT(DISTINCT tg_id)
FROM tg_keywords AS kw1, tg_keywords AS kw2
WHERE kw1.tg_id = kw2.tg_id
AND kw1.tg_keyword='keyword_1'
AND kw2.tg_keyword='keyword_2'
your query can't work because you have a condition which is always false so no record will be selected!
tg_keyword='keyword_1' AND tg_keyword='keyword_2'
what are you trying to do? Could you post the columns of this table?
tg_keyword='keyword_1' AND tg_keyword='keyword_2'
Logically this cannot be true, ever. It cannot be both. Did you mean something like:
SELECT * FROM keywords
WHERE tg_keyword LIKE '%keyword_1%' OR tg_keyword LIKE '%keyword_2%'
ORDER BY tg_keyword LIKE '%keyword_1%' + tg_keyword LIKE '%keyword_2%' DESC;
Based on the OP's clarification:
I have a table with multiple keywords with the same id. How can I get more than one keyword compared for the same id, as the search results need to be based on how many keywords from a search array match keywords in the keywords table from each unique id. Any ideas?
I assume you're looking to return search results based on a ranking of how many of the selected keywords are a match with those results? In other words, is the ID field that multiple keywords share the ID of a potential search result?
If so, assuming you pass in an array of keywords of the form {k1, k2, k3, k4}, you might use a query like this:
SELECT ID, COUNT(ID) AS ResultRank FROM tg_keywords WHERE tg_keyword IN (k1, k2, k3, k4) GROUP BY ID ORDER BY ResultRank DESC
This example also assumes a given keyword might appear in the tables multiple times with different IDs (because a keyword might apply to multiple search results). The query will return a list of IDs in descending order based on the number of times they appear with any of the selected keywords. In the given example, the highest rank for a given ID should be 4, meaning ALL keywords apply to the result with that ID...
I think you will need to join tg_keywords to itself. Try playing around with something like
select *
from tg_keywords k1
join tg_keywords k2 on k1.tg_id = k2.tg_id
where k1.tg_keyword = 'keyword_1' and k2.tg_keyword = 'keyword_2'
Try:
SELECT tg_id
FROM tg_keywords
WHERE tg_keyword in ('keyword_1','keyword_2')
GROUP BY tg_id
HAVING COUNT(DISTINCT tg_keyword) = 2
I have the most simple SQL
SELECT * FROM words
It has 13000 words (varchar). I need to get the longest word first in my output. I guess it might be possible with the WHERE command?
Alternative
If it doesn't work like that, is there a smart way to sort my output array so it will order it by the longest word first (in the 'word'-column). It looks like this (in the output loop)?
$output_array[$row['word']] = $row['another_word'];
Ordering the words by its length should do it:
SELECT *
FROM words
ORDER BY LENGTH(word) DESC
select * from words order by len(my_field) desc;