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.
Related
I am querying a MySQL table, and want it only to output items that match the word but can have other words as well. I am sort of looking for the equivalence of in MySql.
strpos ( string $haystack , string $needle)
PHP myAdmin TABLE Here is an image of my phpMyAdmin table. I am trying to search the table if the rightmost column, the archetype column. I am trying to find all results that match one word like Voltron, but the problem is that for example item #11, also has the word 'Keyword' in that column so it won't show.
Image I'm Searching By This is an example of what I want to search by. The last column, the archetype column is the variable. I want to query MySQL for all items that were Archetype like Tribal, or like Voltron, but can contain other 'Archetypes' as well.
When I query by this card, item number 3,9,10,11, and 15 should be outputted.
$aType = explode(" ", $aType);
$array_in_strings = join(", ",$aType);
//This Decides the Cards.
$query_all_items = mysqli_query($connect, "SELECT * FROM `edh__cardDatabase` WHERE `name` NOT LIKE '$name' AND (`archetype`) IN ('$array_in_strings') ORDER BY `name` ASC;");
And finally, this is what I have as the code.
Implode your words list to CSV string then use a query similar to
SELECT *
FROM {tablename}
WHERE FIND_IN_SET({columnname}, 'CSV,words,list')
AND {another conditions}
Pay attention - if you put a space between a comma and the next word then this space will be treated as a part of a word. I.e. 'word' will be found in 'this,is,a,word' but not found in 'this, is, a, word'.
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().
I am searching from 3 tables currently (will search in more after sorting this out). This query brings all the results in the order of the tables listed in query. Whereas I want to get the most relevant search results first.
(Select name, url, text, 'behandlinger_scat' AS `table` from behandlinger_scat where name LIKE '%KEYWORD%' OR text LIKE '%KEYWORD%')
UNION
(Select name, url, text, 'hudsykdommer_scat' AS `table` from hudsykdommer_scat where name LIKE '%KEYWORD%' OR text LIKE '%KEYWORD%')
UNION
(Select name, url, text, 'om_oss' AS `table` from om_oss where name LIKE '%KEYWORD%' OR text LIKE '%KEYWORD%')
Any help would be appreciated.
You can use a method to order by the points you dynamically give the results, as in this example (you will need to alias your tables so SQL will understand what column you're referring to):
ORDER BY
CASE WHEN name LIKE table.keywords THEN 100 ELSE 0 END +
CASE WHEN name LIKE table2.keywords THEN 10 ELSE 0 END +
CASE WHEN text LIKE table2.keyword THEN 1 ELSE 0 END
DESC
This is merely an example, but the concept is the following:
You decide how many "points" each "match" will receive (e.g name matches keyword is 100 points, text matches it - a little less) then, each row "accumulates" points with correlation to its matches, and the row with the most points shows first.
I have this query but it returns the name of author the number of times it exists in the database ..
$query = "SELECT bauthor FROM info WHERE Cat1 = 'novel'";
$result = MySQL_query($query);
i want the author's name to be displayed once and the number of books he has to be in a bracket ...for example author's name is aaaaa and he has written 20 books so wen i run this query it shows mw his name 20 times but i want it to be in this way aaaa(20)
I can't do much without your full schema, but try using the COUNT feature with a GROUP BY clause, like
SELECT bauthor, COUNT(books) AS numbooks FROM info WHERE ... GROUP BY bauthor
EDIT: See this SQLFiddle for an example: http://sqlfiddle.com/#!2/bb5f5/1/0
Right now i have a column called full_name where the first and last name gets stored. It is seperated by a normal space, so a name is stored like this: "Firstname Lastname". Now when im making this search users function, i have this:
$query = "SELECT full_name, id, user_name, sex, last_access, bostadsort FROM users WHERE full_name LIKE '$searchUser%'";
It works great by finding if you search by the firstname. I wish to make if you e.g type "lastname" then the user also will come up.
Now i find this quite my fault, that i started by having 1 column for the full_name and not firstname lastname columns.
So i wonder if i can do this without making two new columns and change rest of my code to work with firstname lastname new columns.. ?
Im using MySQL
full_name LIKE '$name%' OR full_name LIKE '% $name'
You should however split this column into two separate columns. What about multiple forenames/surnames?
My recommendation is to get them in 2 separate fields. Otherwise use % wildcard as well in the beginning of the string.
... WHERE full_name LIKE '$searchUser%' OR full_name LIKE '%$searchUser' OR full_name LIKE '%$searchUser%'
will that do?
Put a % in front of your variable to denote you want to match items at the end, a % in the back to denote you want to match the items in the front, and a % in front and back to denote the item you want to match is in the middle.