Show each number separated after comma in php - php

I have two tables
category
id | category
------
1 | One
2 | Two
etc
second one is
mp_photos
id|name|test|CategoryID
1 |name|test|1,2
So now i need to fetch data from mp_photos row CategoryID (1,2) every categoryid is seppareted by comma and link every number with Category name in first table

Best option would be to restructure your database and create another table with id_mp_photos and id_category and use joins.
If you cannot do it there are two options (both are slow):
use explode() in php
use MySQL function FIND_IN_SET(), for example:
SELECT mp_photos.name, mp_photos.test, category.category
FROM mp_photos JOIN category ON FIND_IN_SET(category.id, mp_photos.CategoryID) > 0

Related

Specific SQL issue: How to select rows where ALL IDs match... (MySQL)

I have this very specific problem which I can't even decide how to approach. So I have 3 tables in MySQL.
Table recipe: id_recipe| name | text | picture
Table ingredients_recipe: id_rs | id_recipe| id_ingredients
Table ingredients: id_ingredient | name | picutre
This is a site, where you select ingredients(so the input is 1 or more id_ingredient) and it should display three categories:
All recipes you can make right now (you have all the ingredients required for it)
All recipes where you are missing only 1 or 2 ingredients
All recipes where you are missing only 3 or 4 ingredients.
Can you help me with these 3 SQL selects? I'm pretty deadlocked right now. Thanks.
SAMPLE DATA: http://pastebin.com/aTC5kQJi
I think your basic statement is already on the right track. You just need to do a little trick. You cannot compare them directly, but you can compare the count of ingredients:
SELECT id_receipe, count(id_rs) as ingredient_count
FROM ingredients_recipe
WHERE id_ingredient IN ( 2, 5)
GROUP BY id_recipe
This will give you the count of ingredients you have for each receipe. Now get the total amount of ingredients for each receipe
SELECT id_receipe, count(id_rs) as ingredient_count
FROM ingredients_recipe
GROUP BY id_recipe
an compare them. Taking the first query as a basis. You can easily get your three different categories out of this.

Display the article in the database base on categories

I am having a database with a table to stock the products of my site and the table has the following column for all the categories a specific product belongs to:
After making a query to get the categories the first row belongs to, The information is stored in an object which looks like this $productDetails->ProductCategoryID and the content is the following: dinner,casual,kids
Now,my question is that how can I use the SQL command SELECT to get all the products having at least one category in common by using PHP.
Kindly help me solve this problem. Sorry I am not a native english speaker
If I've understood, you have a column which contains a string representing product's categories separated by comma. In this case you have to execute a substring function on the column ProductCategoryID, which is always discouraged.
I suggest you, instead of using the column ProductTable.ProductCategoryID, to make a link table ProductsCategories with the columns ProductID, CategoryID.
----------------------
| ProductsCategories |
----------------------
| ProductID |
| CategoryID |
----------------------
In this way you can use a more efficient QUERY like this one:
SELECT DISTINCT ProductTable.*
FROM ProductTable
INNER JOIN ProductsCategories ON (ProductsCategories.ProductID = ProductTable.ProductID)
WHERE CategoryID IN
(
SELECT CategoryID
FROM ProductsCategories
WHERE ProductID != ProductTable.ProductID
)
;
All this, of course, has sense if I've understood in the right way the structure of ProductCategoryID column :)

UPDATE multiple MySQL rows with different values

Note: I realize this may be confusing taking about tables and columns below so here is a simplified version of the two tables I mention:
Table "categories" has columns: id, type
Table "entries" has columns: id, categories, ...
I have a MySQL table named entries where one of the columns, named categories, is a string of pipe separated indices. For example, "|1|2|3|" might be a possible value and "|1|3|4|" could be another. The numbers represent indices of the table categories.
I'm writing an admin script that allows a new row to be inserted into the categories table and when doing this, the appropriate rows of the table entires should have the categories column updated. For example, if a newly inserted row of the categories table has index 5 then "5|" should be concatenated to each appropriate column categories of the entries table.
I realize that I could could use UPDATE per appropriate entries row when adding a new category but am wondering if there is a better way to go about this. In case this is not clear I know that this is an option but want to know if this can be made into one statement (pseudo-code):
foreach ($entriesToUpdate as $currEntry)
"UPDATE entires SET categories='".$currValue."|".$newIndex."' WHERE id=".$currId;
This can be done with an expression-based update:
UPDATE entries SET categories=CONCAT(categories, "5|") WHERE id IN (1,2,3,4,...)
(5| instead of 5| from your example, since your examples seem to show that the existing value will start and end with |s already.)
That said, you'd probably be better off with a database schema that stores a mapping of entries to categories in a separate many-to-many table. For example:
categories:
id | type | ...
---------------
1 ...
2 ...
3 ...
entries:
id | ...
---------
100 ...
101 ...
102 ...
entries_to_categories:
entry | category
----------------
100 1
100 2
101 3
Then you can use JOINs to retrieve the set of categories if desired, or check if something is in a category. In this case, entry 100 is in categories 1 and 2, entry 101 is in category 3, and entry 102 is in no categories.

Use search results (Like %search%), to match id's in another table in the database

I have two tables in the database, parts, and products.
I have a column in the products table with strings of ids (comma separated). Those ids match ids of the parts table.
**parts**
ID | description (I'm searching this part)
-------------------------------
1 | some text here
2 | some different text here
3 | ect...
**products**
ID | parts-list
--------------------------------
1 | 1,2,3
2 | 2,3
3 | 1,2
I'm really struggling with the SQL query on this one.
I've done the 1st part, got the id's from the parts table
SELECT * FROM parts WHERE description LIKE '%{$search}%'
The biggest problem is the comma separated structure of the the description column.
Obviously, I could do it in PHP, create an array of the the results from the parts table, use that to search the products table for id's, and then use those results to grab the row data from the parts table (again). Not very efficient.
I also tried this, but I'm obviously trying to compare two arrays here, not sure how this should be done.
SELECT * FROM `products` WHERE
CONCAT(',', description, ',')
IN (SELECT `id` FROM `parts` WHERE `description` LIKE '%{$search}%')
Can anybody help?
I would perhaps try a combination of LOCATE() and SUBSTR(). I work mainly in MSSQL which has CHARINDEX() that I think works like MySQL's LOCATE(). It is bound to be messy. Are there a variable number of elements in the parts-list field?

How do you store and search number intervals in MySQL/PHP?

I have the following scenario:
The user will input an interval and/or single comma-separated number codes for each product.
Partial table example:
name | codes
-----------------------------------
Product 1 | 239,300-350
Product 2 | 430-450,500,29
Product 3 | 780,2
For example, when the user searches for code 321, it should return the row of Product 1.
Is there a way for doing that search with a single query?
I'd suggest doing this with two tables instead:
products:
- name
- id
code_ranges:
- product_id
- range_start
- range_end
Then you can get what you want (for some $input_id) by using a join:
SELECT product.id, product.name
FROM products
JOIN code_ranges ON products.id = code_ranges.product_id
WHERE code_ranges.range_start <= $input_id
AND code_ranges.range_end >= $input_id
You can have multiple rows in the code_ranges table for each product to represent multiple different ranges (e.g. 2-4,7-10 would have two rows, one with range_start=2 and range_end=4, and another for the 7-10 range).
A product code that isn't a range would simply be treated as a range where the start and end are the same (e.g. 7 would be range_start=7 and range_end=7).
I have same problem but that first answer is not helpful.Because when you looking for item code range=239 then you can't difine that item also contains 300-350

Categories