I have three tables called product, product_category and category with product_category being the linking table.
How can I join these tables using SQL in PHP?
The linking table has only productID linking to the product table and catID linking to the category.
Something like this?
SELECT
*
FROM
product
INNER JOIN
product_category
ON product_category.productID = product.productID
INNER JOIN
category
ON category.catID = product_category.catID
Your query should look something like this:
Added the requirement that there should be a productId and categoryId from a variable:
$query = "SELECT * FROM
product p
JOIN product_category pc ON p.id = pc.productId
JOIN category c ON c.id = pc. categoryId
WHERE p.id = {$productId}
AND c.id = {$categoryId}";
Related
I have 3 tables products , category and product_category here are strutures of tables
products = id(pk), name , sku, ...
category = id(pk),name,parent_id(self relation)
product_category = category_id(fk), product_id(fk)
here is my query so for running
SELECT `products`.*,`product_category`.`category_id` as catid FROM `products` LEFT JOIN `product_category` ON products.id = product_category.item_id GROUP BY `products`.`id`
it brings all products and category_id from product_category table i want data from category table as well like category.name . how i can do this?
Is this not as simple as..?
SELECT *
FROM products p
JOIN product_category pc ON p.product_id = pc.product_ID
JOIN category c ON pc.category_id = c.category_id;
I have little problem with SQL query in PHP.
I want to get from table post rows with category ex. 'art'.
This table don't contain name of my category (only category_id).
So, how to connect this in my query?
Tables:
post:
id, title, category_id
category:
id, name
I tried this way, but it not works.
SELECT * FROM post WHERE category_id = category.id AND category.name="art";
Anyone can help me? Thanks.
You need to join the tables.
Given your schema, this should give you an starting point:
SELECT p.* FROM post p INNER JOIN category c ON p.category_id = c.id WHERE c.name = 'art';
You are missing the JOIN
SELECT * FROM post
INNER JOIN category
WHERE category_id = category.id AND category.name="art";
You need to use join to query on name column of category table, e.g.:
SELECT p.*
FROM post p JOIN category c ON p.category_id = c.id
WHERE c.name = 'art';
This can be question which can ask on this site but I unable to find answer for it.
My question is that I have 3 tables
a . Product table
b. category table
c. product_category table
Any product can have multiple categories
So if i want to know all products which contain any category so i can use mysql query like
Select *
From products
left join product_category on (product.id = product_category.product_id)
Where category id in (45,56,78)
but if I want all only those products which have all these categories then how i find it ?
Select p.id
from products p
join product_category pc on p.id = pc.product_id
where category_id in (45,56,78)
group by p.id
having count(distinct category_id) = 3
I have a problem with MySQL statement:
SELECT
oxarticles.OXTITLE AS TITLE,
oxmanufacturers.OXTITLE_1 AS OXMANTITLE,
oxarticles.OXDISTEAN AS OXDISTEAN,
oxarticles.OXMPN AS MPN,
oxarticles.OXPRICE AS OXPRICE,
oxarticles.OXSTOCK AS OXSTOCK,
oxarticles.OXARTNUM AS OXARTNUM,
oxseo.OXSEOURL AS OXSEOURL,
oxartextends.OXLONGDESC_1 AS OXLONGDESC
FROM `oxarticles`
INNER JOIN `oxartextends` ON oxarticles.OXID = oxartextends.OXID
INNER JOIN `oxmanufacturers` ON oxarticles.OXID = oxmanufacturers.OXID
INNER JOIN `oxseo` ON oxarticles.OXID = oxseo.OXOBJECTID;
My problem is that tables oxarticles and oxmanufacturers have two same column names OXID and OXTITLE_1 but the above code doesn't work. Please help.
You are trying to match article id to manufacturer id. That's wrong. You need to join your article table to the manufacturer table using the manufacturer id, not the article id.
In your case, that is oxarticles.OXMANUFACTURERID for the manufacturer id in the article table and oxmanufacturers.OXID in the manufacturer table.
SELECT
oxarticles.OXTITLE AS TITLE,
oxmanufacturers.OXTITLE_1 AS OXMANTITLE,
oxarticles.OXDISTEAN AS OXDISTEAN,
oxarticles.OXMPN AS MPN,
oxarticles.OXPRICE AS OXPRICE,
oxarticles.OXSTOCK AS OXSTOCK,
oxarticles.OXARTNUM AS OXARTNUM,
oxseo.OXSEOURL AS OXSEOURL,
oxartextends.OXLONGDESC_1 AS OXLONGDESC
FROM `oxarticles`
INNER JOIN `oxartextends`
ON oxarticles.OXID = oxartextends.OXID
INNER JOIN `oxmanufacturers`
ON oxarticles.OXMANUFACTURERID = oxmanufacturers.OXID
-- ^^^
-- Here's the manufacturer id
-- in the article table
INNER JOIN `oxseo`
ON oxarticles.OXID = oxseo.OXOBJECTID;
I have 3 tables
products categories products_categories
------------ ------------- --------------------
product_id category_id product_id
product_title category_title category_id
Each product can belong to multiple categories as the db schema shows.
I join the three tables to get a listing of the three last inserted products.
SELECT p.product_id,
p.product_title,
c.category_id,
c.category_title
FROM products p
INNER JOIN products_categories pc ON p.product_id = pc.product_id
INNER JOIN categories c ON pc.category_id = c.category_id GROUP BY p.product_id LIMIT 3
With the above query I get the products but with the first category that the product belongs to.
My question is, is it possible by modifying the query to get all the categories that each product belongs to, in one row along with the product info?
Or the only way is to execute another query for each product while fetching the list to get its categories?
You could try:
SELECT p.product_id, p.product_title,
GROUP_CONCAT(c.category_title)
FROM products p
INNER JOIN products_categories pc ON p.product_id = pc.product_id
INNER JOIN categories c ON pc.category_id = c.category_id
GROUP BY p.product_id
See GROUP_CONCAT syntax here
Dont use "Group By". Then in your PHP Code group manually to product_id and collect all categories.