php&mysql find parent category name - php

I have problem about inner join, left join commands.
My category table is:
ID | parent | title
1 | 0 | First Category
2 | 1 | Other Category
I have list categorys and I want get parents category title at sql command.
I have tried:
SELECT cat.ID, cat.title, cat2.title as parentcatname, cat.parent
FROM categories cat INNER JOIN categories cat2 ON cat2.ID=cat.parent
But ıt's not working.

You have to use LEFT JOIN to be able to pull all categories no matter have they parent category or not. INNER JOIN filters out all mismatches.
SELECT c.id, c.title, c.parent, p.title parent_title
FROM categories c LEFT JOIN categories p
ON c.parent = p.id
Output:
| ID | TITLE | PARENT | PARENT_TITLE |
-------------------------------------------------
| 1 | First Category | 0 | (null) |
| 2 | Other Category | 1 | First Category |
Here is SQLFiddle demo

If you want to get all parent categories then try query
SELECT cat_id FROM categories WHERE parent=0;
If you want get parent category of a category
SELECT C.cat_id, P.title FROM categories C LEFT JOIN categories P ON P.parent=C.cat_id;
I haven't tested above code but it should work fine.

You can always debug your SQL by entering it into a validator (loads online), phpMyAdmin's SQL tab or a editor with SQL validation. It looks to me like you have a small typo near your categories table selection.
Always dumb it down if your SQL isn't working. Note that JOINs (inner, left, right, ect) are meant to join TWO or MORE tables.
SELECT
one.ID, one.title, one.parent, one.title, one.parent, one.title
FROM
categories one
LEFT JOIN
categories two
ON
one.parent = two.ID

Related

Dynamic Query to Select Multiple Entries from a Relational Record Table

Let's say I have a table which stores the relation between products and their categories:
p_id | c_id
-----+-----
1 | 1
1 | 2
2 | 1
2 | 2
2 | 3
3 | 2
As you can see, a product might have multiple categories. How can I search for products that have categories 1 and 2 assigned? The closest I can think of is using JOIN:
SELECT a.p_id
FROM rel_table a
JOIN rel table b
ON a.p_id=b.p_id AND b.c_id=2
WHERE a.c_id=1
While this achieves what I want, it is not practical because my query will be dynamic. If I have to select products with 3 categories, this requires a difficult change in the query.
Is there a cleaner and more clever way to achieve this? I imagine something that selects first set, then refines with another category for the amount of levels needed.
You should use IN or Between for such things. You can dynamically create the values you put in the IN/BETWEEN
SELECT a.p_id
FROM rel_table a
WHERE a.c_id IN (1,2,3)
group by a.p_id
having count(1) = 3
order by a.p_id asc
or
SELECT a.p_id
FROM rel_table a
WHERE a.c_id between 1 and 3
group by a.p_id
having count(1) = 3
order by a.p_id asc

SQL not Returning Desired Results

I am trying to generate result from a SQL, but i am not getting desired results.
above image is the result of following query
SELECT DISTINCT
U.USERID, U.shopName, U.image, P.PID
FROM users U, products P
WHERE P.USERID=U.USERID
GROUP BY U.USERID
ORDER BY P.PID DESC
Now what i want to do is that there is another record for USERID 2 and 3 with PID 3 and 5 respectively, let me show you the image
I want to retrieve latest record entered in the table but with distinct USERID
e-g i want to retrive this
userid | shopname | image | PID
3 | shop name | image | 5
4 | shop name | image | 4
2 | shop name | image | 3
any help will be appreciable.
select * from "table" grou by ID and order by Desc
Use it like below, Do group by with PID.
SELECT DISTINCT U.USERID, U.shopName, U.image, P.PID FROM users U, products P WHERE P.USERID=U.USERID GROUP BY P.PID ORDER BY P.PID DESC
I am not sure if I understood your problem, but perhaps the following query solves your problem:
SELECT U.USERID, U.shopName, U.image, P.PID
FROM users U, products P
WHERE P.USERID=U.USERID
AND P.PID in (select min(p2.pid) from products p2 where u.userid = p2.userid)
A word of warning: it might be more efficient to use the ORDER BY trick and filter the results externally.

MySQL multiple WHERE condition

I'm trying to get this output with MySQL:
-Kate
-William
-dog
(3 results)
From those databases:
--------------- ----------------------
| users | | pet |
|-------------| |------|------|-------
| id | Name | | user|animal|number|
|-------------| |------|------|------|
| 1 |Kate | | 1 | cat | 0 |
| 2 |William| | 2 | dog | 1 |
--------------- ----------------------
Number needs to be != 0
and I need to be able to make the difference between a fetch where number = 0 and number != 0
SELECT
name, animal
FROM
users
INNER JOIN pet ON users.id = pet.user
WHERE
number != 0'
I can't get 'Kate' because never matching != 0.
I think I should use 2 different WHERE conditions in one request, but I don't know how...
First give your tables an alias because you have a ambigious column name id.
SELECT u.name, p.animal
FROM users AS u
INNER JOIN pet AS p
ON u.id = p.id
WHERE p.number != 0
But what you asking for is to get results from both tables without join, right? Like this
SELECT
name AS creatur,
'users' AS type
FROM users
UNION
SELECT
animal AS creatur,
'pet' AS type
FROM pet
WHERE number != 0
First. Use aliases for tables. Use, for example, 'u' for users table and 'p' for 'pet'. Mysql do not understand which field get from which table without aliases.
Second. Condition in WHERE section not related to JOIN. Use AND in ON section.
Third. Use LEFT JOIN instead of INNER JOIN and read articles about types of JOIN and differency between them.
Fourth. In such cases in users table usually adding field pet_id. Do not use single id field for both entities. This thing named ONE-TO-MANY relation. If you want use MANY-TO-MANY relation, you must add third table with two fields user_id and pet_id.
But this query may solve your question:
SELECT u.Name, p.animal
FROM users AS u
LEFT JOIN pet AS p ON u.id = p.user AND p.number != 0

MySQL Query: query the PRODUCTS table (among others) and bring up BOTH the category AND subcategory

I wonder if someone could help me with a MySQL (v5.0.51) query I am having trouble with. I have a table like this:
category_id | category_text | subcategory
25 | Motor Controls | NULL
26 | Contactors | 25
27 | Overloads | 25
28 | Motors | NULL
29 | Accessories | 28
Each entry with NULL in the subcategory column represents a top-level category and the value in the subcategory tells which top-level category the subcategory belongs to.
For example above:
Contactors and Overloads belongs to Motor Control because their respective subcategory has a value of 25 which is the category_id (25) of Motor Controls
Now I use these values in a PRODUCTS table to store products but here I ONLY store the category_id of the subcategory.
product_id | product_item | product_supplier_id | product_description | product_category_id
24 | Product A | 39 | Product A description | 27 --category_id of subcategory (ie.Overloads)
25 | Product B | 39 | Product B description | 26 --(Contactors)
The problem I am having is that I need one SQL query where I can query the PRODUCTS table (among others) and bring up BOTH the category AND subcategory. I can do it for one or the other but not both. I have tried UNION, JOINS etc but just cannot seem to get it. This is what I have so far which just brings up the subcategory.
SELECT p.*, c.company_name, pc.category_text AS subcategory
FROM products p, company c, product_categories pc
WHERE p.product_supplier_id = c.company_id
AND p.product_category_id = pc.category_id
This results in the query generating the result below.
product_id | product_item | product_supplier_id | product_description | product_category_id | company_name | subcategory
24 | Product A | 39 | Product A description | 27 | Product A Supplier | Overloads
25 | Product B | 39 | Product B description | 26 | Product B Supplier | Contactors
Basically what I want is to add a column that contains the correct top-level category for each subcategory so it looks like this
24 | Product A | 39 | Product A description | 27 | Product A Supplier | **Motor Controls** | Overloads
Can I do this with the way the table is created or do I need to modify it?
Sorry I had some nice images to make it easier to understand but with a "reputation" of 1 , I couldn't post it.
Any help would be appreciated. Please note that I am using the query in PHP to access the MySQL database.
SELECT p.*,
c.company_name,
pc.category_text AS subcategory
ppc.category_text AS category
FROM products p,
company c,
product_categories pc,
product_categories ppc
WHERE p.product_supplier_id = c.company_id
AND p.product_category_id = pc.category_id
AND pc.subcategory = ppc.category_id
P.S: I wish MySQL had a better support for querying hierarchical data (like connect by clause in Oracle)
SELECT p.*, c.company_name, pc.category_text AS subcategory,ppc.category_text AS category
FROM products p
left join company c on p.product_supplier_id = c.company_id
left join product_categories pc on p.product_category_id = pc.category_id
left join product_categories ppc on pc.subcategory = ppc.category_id

related tables with many levels

Im trying make a menu with the next structure:
-category 1
--category 1.1
---category 1.1.1
----product 1
----product 2
----product 3
-category 2
--category 2.1
---category 2.1.1
----product 1
----product 2
----product 3
-category 3
--category 3.1
---category 3.1.1
----product 1
I have an scheme like wordpress:
relation_category_products table store an id from category and id from products.
The question is whats the best way to make the mysql queries for having all that structure?
My first solution it was make queries for parent category (-category), then for each every row take the id for the next --category and then until raise the product node. But with that tecnique theres a lot of queries (35 for the moment). And i dont know whats the better way to get all that relationship and take it with php for render the menu.
thanks
http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
Your table structure should have a field for the parent, e.g.
Table "category"
id(int) name parent(int)
1 Category 1
2 Category 1.1 1
3 Category 1.1.1 2
4 Category 2
5 Category 2.1 4
You then use a one-to-many relationship in your "Product" table to link them to a category.
If you want to retrieve the nodes directly under a category, just SELECT by the "parent" field.
If you want to retrieve the whole tree use a join query:
SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3
FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.id
LEFT JOIN category AS t3 ON t3.parent = t2.id
This will give you back something like:
+-------------+----------------------+---------------+
| lev1 | lev2 | lev3 |
+-------------+----------------------+---------------+
| Category 1 | Category 1.1 | Category 1.1.1|
| Category 2 | Category 2.1 | NULL |
+-------------+----------------------+---------------+
You could use a leftjoin and then loop the array with a foreach make sure you croup them correctly and use the primary column alt. an indexed one for best performance there is a good guide for that here
regards

Categories