Splitting strings in mysql - php

Ok so I have looked up several ways of getting this done, and none of them work for me, let me give you a few details on why the methods I have tried don't work.
I have tried SUBSTRING_INDEX and as most of us know it only "splits" based on the number of delimiters there are in the string, and well it isn't the same amount for all of the rows, so that is a no go, I also tried the method found here http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
Again I believe that it goes based on the number of delimiters in your query.
I have searched and searched for a way to get this done, and I just can't find a way to get it working. I am still a bit of a newbie to PHP so this could be a simple fix and I am just overlooking it somehow, but that I am not sure of.
Here is what I am working with;
This is the sql query
$sql = "SELECT * FROM offers WHERE type='$type' and country='$membercountry' LIMIT $start, $limit";
$result = mysql_query($sql);
country is the column that has my comma delimited string. Some rows simply just have something like US while others have US,AU,CA,GB
The reason I am needing to split these is because the page that actually displays the offers checks to see if the users country matches the offers country, and it is obviously going to not match when it is reading it as a whole long string, Which means those offers won't show up for the user.
I talked to a friend of mine and he said to use explode(), but I am pretty sure you cannot use explode() in a sql query.
I know I probably haven't explained this as well as I could have, I have been working on this project all night so I am a bit tired.
Also as a note, I know that I could just make it to where there are multiple rows for each offer supplying the different country, but some of these offers allow all countries to do them, that would mean that for 1 offer I would have like 243 rows, and that would make me feel like I was just being sloppy!
If anyone could give me a push in the right direction I would be greatly appreciative!
Thanks, Casey

Bit of an odd/bad data design: multiple countries in a column named country. That aside, you could maybe do something with FIND_IN_SET().
mysql> SELECT * FROM offers WHERE FIND_IN_SET('CA', country);
Performance wise .. it's not going to be great but I hope you can select on other fields using a good index.

Something like this may work out:
$sql = "SELECT * FROM offers WHERE type='$type' and country LIKE '%$membercountry%' LIMIT $start, $limit";

Related

PHP/SQL Check if a similar entry already exists in the database

I have a search on my website and im trying to show a list of the most popular search terms on my site it sort of works but it isn't matching the strings close enough.
This is what i'm currently using:
$sql = "SELECT * FROM db WHERE r_name LIKE '%".$searchname."%' OR r_number like '%".$searchname."%'
However if a user searches for say Game Name and another searches for Game Name (Reviews) it will add 2 entries into my database, Is there a way to do a similarity test before entering the entry ?
yes, there is a way but it requires you to fiddle with it a little. there is a search called
SOUNDEX, which will match things that are very close. It might not be a perfect solution to your question, but it is definitely something that might get you started in the right direction.
SELECT * FROM db WHERE SOUNDEX( db.r_name ) LIKE SOUNDEX( '{$searchname}' );
I believe that if you have an entry 'lowercasexd' , and do soundex like('what is lowercasexd?'), it will find the entry that's associated with 'lowercasexd'.
Be aware that this type of search take a little while to run compare to '=' searches on indexed databases(on my database it does about 5-6k entries per second) so it is NOT recommended for anything big. If you want a near-perfect solution, I suggest you read about google's search mechanism, and look up some search engine source code if your project is significant enough.

PHP/MySQL join with CSV array

I have a tricky (well for me it is) question about getting info from 2 different tables.
I have an array, like this:
$abc=$_COOKIE['cookie'];
$comma_separated = implode(",", $abc);
Now, the array will be a CSV list of ID's that need to match ID's from the "Specials" table. In the "Specials" table there is a column called Contract Name. This Contract Name column needs to match a column of the same name, each unique, in the Products table, and display the information contained therein.
My currenty MySQL query looks like this:
$query= "SELECT specials.id,
specials.product_name,
specials.contract_name,
specials.included_value AS inc_val,
specials.image_url,
specials.contract_monthly,
specials.outlet,
products.package,
products.bundled,
products.included_value
FROM specials,
product
WHERE `id` IN ('.$comma_separated.')
AND specials.contract_name = `products.package`";
What happens is... that nothing happens. I've tried wrapping my brain around some of the JOIN tutorials but no luck.
So basically I'd like to display a list of current specials, along with the package info, which is contained in a different table. I've tried wrapping my brain around some of the JOIN tutorials but no luck.
To my knowledge I'm crap at explaining things properly, so please do shout if I can shed any more light on this conundrum.
Thanks! :)
To even start to get this to work, your $query string is going to have to be a valid query. Ordinarily, to troubleshoot this kind of problem, you do echo $query; somewhere along the way to see if it is valid. You then might even paste the query's textinto a standalone MySQL client (phpMyAdmin, or maybe HeidiSQL or something) to see what you get.
Looking at this line:
WHERE `id` IN ('.$comma_separated.') /* wrong! */
it looks like it needs to read
WHERE `id` IN (".$comma_separated.")
because you're using double quotes to surround your fragments of SQL text. Also, you might want to use
WHERE `specials`.`id` IN (".$comma_separated.")
just because some other tables might contain id columns and then your search clause will be ambiguous.

MYSQL like + group

Well I'm having a problem mainly caused by bad structure in database. I'm coding this for a company whose code is quite messy and the table is very large so I don't think it's an option to fix the structure.
Anyway, my issue is that I'm trying to somehow group a value that won't be alone in the string...
They are storing values separated with commas... So it would be like
field: "category" value: 'var1, var2, var3'
And I will search using this query:
SELECT name, category
FROM companies
WHERE (MATCH(name, category) AGAINST ('$search' IN BOOLEAN MODE)
OR category LIKE '$search%')
It would match with for example var2 (it's not limited to 3 variables though, can be solo or many more) and I'd split it manually in PHP, no problem. Although I will not get enough matches, I want e.g. 10 matches by different searches. To be more specific I'm making an autosuggest feature, which means I will for example want to match "moto%" with motorbike, motor alone or whatever but I keep getting the same values, like there'd be a couple of 100 of results that contains "motorbike" and I don't know how to filter them, as I'm not able to use GROUP BY due to bad db structure...
I found this: T-SQL - GROUP BY with LIKE - is this possible?
It SEEMED as something that would be a solution, but as far as I've tried I could not get it work with what I wanted.
So I'm wondering which solutions there are... If there are ABSOLUTELY no way of working this around I might probably have to fix the db structure (but this really has to be the last option)
Start taking steps to make database structure proper. Make an extra table and fill it with split values.
Then you can use proper queries to select the data you need. Both you and next developer will have less troubles with this project in the future, not mentioning queries speed gain.
I am not sure why i cannot write a comment, but maybe you can try this:
SELECT name, category FROM companies WHERE category LIKE '$search%' or LOCATE('search', category)>0;
That would look if in category appears any of your 'search' value.
I would have to agree that you should make the database right. It'll save you much trouble and time later. However, using SELECT DISTINCT may fix your immediate issue.

MySQL MATCH-AGAINST plural words?

I'd like to add a search to my site. I have a database of challenges from a video game. Each challenge has a title and description, I'd like to be able to search at least the description, but both if possible. Now, I've set the table up so that I can use MATCH() AGAINST(), but I'm having a problem with words that can be either singular or plural.
For example, the word "assists" appears in multiple challenges, but if the user types "assist", he won't get anything. Is there any way that I can add that functionalty? I've tried everything I can think of, but nothing has worked so far.
Update: I only just recently learned about MATCH AGAINST, so I'm not sure the "right" way to use it in my case. Like I said, I've got a table with a column called description, using the example word from above, assists, which appears multiple times in the table, I would use this query:
SELECT * FROM challenges WHERE MATCH(description) AGAINST('assists')
I just executed that and it returned 10 rows. If I change it to assist, I get nothing.
Maybe you can use the SOUNDEX() function from MySQL.
SELECT
id,
title
FROM products AS p
WHERE p.title SOUNDS LIKE 'Shaw'
// This wil match 'Saw' etc.
I think this also may be applicable for you and that he will take with plurals
I might be missing the point of what you're trying to do, but why not just do a LIKE match like this:
SELECT * FROM challenges WHERE description LIKE "%assist%";
That will match anything that contains "assist" as well as "assists". Of course it will also match "assistant" and "assistance", which may not be what you want...

mysql group duplicates based on title and description

Indeed.com groups duplicate job postings by title and description. Here is an example of what I am talking about. How would I go about doing something like that? Is it just a simple Group By statement or something else entirely?
It could be done with a simple group by, but that will only group exact matches.
There are several parameters you can test to determine whether to group entries. In their example: company name, location, and keywords.
"Something else entirely" would involve analyzing the fields of one row to determine their similarity to another row. I think this would probably be too processor intensive to integrate on a large-scale.
I'm not exactly sure what you're looking at in the example. But it wouldn't really make sense to do a sql group on something like description. That would cause a ton of overhead, especially with the amount of data indeed is keeping track of.
A good way to store data similar to what indeed stores would be with document index, try googling solr or nosql.

Categories