select from 2 different mysql tables - php

I have 2 tables one for products and one for images i need to get all products with its related images. and all products with only one image for each

get all products with its related images
Do a inner join like
select p.product_title,
i.*
from products p join images i on p.product_id = i.image_product;
all products with only one image for each
Perform a group by and then join like
select p.*
from products p join (
select img_product
from images
group by img_product
having count(distinct img_product) = 1) xxx on p.product_id = xxx.img_product;

Related

Limit values for the left Join part of a query

Please i have four tables joined together using the LEFT JOIN, the images table is linked to the items table by img_item, thus each item can have more images. i want to fetch only the first image of every item. How do i go achieve this.
SELECT * FROM items
LEFT JOIN category ON items.item_cat = category.cat_id
LEFT JOIN users ON users.user_id=items.item_user
LEFT JOIN institutions ON institutions.inst_id=users.user_inst
LEFT JOIN images ON images.img_item = items.item_id
ORDER BY item_id DESC
In MySQL, you can enumerate the results using variables, and then choose the first. Another alternative is to identify which one you want, and choose that one. The following chooses the image with the largest id:
SELECT *
FROM items LEFT JOIN
category
ON items.item_cat = category.cat_id LEFT JOIN
users
ON users.user_id=items.item_user LEFT JOIN
institutions
ON institutions.inst_id = users.user_inst LEFT JOIN
images
ON images.img_item = items.item_id AND
images.img_id = (SELECT MAX(i2.img_id)
FROM images i2
WHERE i2.img_item = images.img_item
);
ORDER BY item_id DESC

MySQL - WHERE / HAVING count

I searched lots of similar topics about this but can't find the answer to this specific probem.
So I have a table with categories and I have another table with products, so what I want is select all the categories that contain at least 1 product, seems very easy but the following code dont give me what I expect.
SELECT *
FROM categories
INNER JOIN products on (categories.id = products.cat_id)
HAVING count(products.cat_id) > 0
All help is appreciated.
Thank you!
You can actually get what you want with just a join because any categories without products will not match.
You should also add a DISTINCT or GROUP BY to remove the duplicate category records from the results:
SELECT DISTINCT c.*
FROM categories c
JOIN products p
ON c.id = p.cat_id
OR:
SELECT c.*
FROM categories c
JOIN products p
ON c.id = p.cat_id
GROUP BY c.id
If you want something fancy like categories that have 2 or more products then you can use GROUP BY and HAVING:
SELECT c.*
FROM categories c
JOIN products p
ON c.id = p.cat_id
GROUP BY c.id
HAVING count(*) >= 2
Try this select in this select all categories that have at least one product
select categories.* from categories
left join products on (categories.id = products.cat_id)
where products.cat_id IS NOT NULL
group by categories.id
In this case you're not using "HAVING" but will have the same result

Selecting multiple counts with left outer join

I've got 3 tables: Product, Shares, Likes which are connected by ProductID. What I want to do is to select all products and COUNT(shares) and COUNT(likes) of these products in one query.
First of all is it possible to do with just one query? If possible how can i do it? And most importantly should I select all products and display then when users hover on make an Ajax call and get like and share data? Thanks in advance.
Here you go:
SELECT
p.id AS 'product id',
IFNULL(COUNT(DISTINCT s.id), 0) AS 'total shares',
IFNULL(COUNT(DISTINCT l.id), 0) AS 'total likes'
FROM
products p
LEFT JOIN shares s ON s.product_id = p.id
LEFT JOIN likes l ON l.product_id = p.id
GROUP BY p.id
IFNULL will treat cases when there are no shares or likes; DISTINCT should be there because otherwise one share will be counted multiple times when joined with likes (and vice versa).
Its possible to do it on one query. Off the top of my head something like
SELECT
product_id,
COUNT(likes.product_id) as likes,
COUNT(SELECT share_id FROM shares WHERE product_id = p.product_id) as shares
FROM product p
LEFT JOIN likes USING(product_id)
Do watch performance though. Make sure you have your indexes etc
SELECT
p.ProductID,
COUNT(s.ProductID) AS SharesCount,
COUNT(l.ProductID) AS LikesCount
FROM
Products p LEFT JOIN Shares s
ON P.ProductID = s.ProductID
LEFT JOIN likes l
ON P.ProductID = l.ProductID
GROUP BY p.ProductID

Fetching product categories in one row with product info

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.

problem in php mysql

i want to get distinct category name from both photos & videos tables which have category name as in categories table...
here, m trying to execute a mysql query to get distinct categories name from main category table which are coming in both or even in one table i.e. photos & videos...
mysql query:
SELECT DISTINCT t1.category_name FROM categories t1, photos t2, videos t3 WHERE
t1.category_name=t2.category OR t1.category_name=t3.category AND t2.block=0 AND
t3.block=0 ORDER BY t1.category_name asc
here's the structure of all tables...
CATEGORIES:: (MAIN TABLE)
PHOTOS:: (CONTAINS PHOTOS OF ABOVE CATEGORIES)
VIDEOS:: (CONTAINS VIDEOS OF ABOVE CATEGORIES)
please, help me...
I'd use an EXISTS clause
SELECT c.category_name
FROM categories c
WHERE EXISTS (
SELECT 1 FROM photos p
WHERE p.category = c.category_name
UNION
SELECT 1 FROM videos v
WHERE v.category = c.category_name
)
If i understand the question right this would work:
select distinct categories.name
from categories
left join photos on photos.category = categories.name
left join videos on videos.category = categories.name
where (photos.id IS NOT NULL) OR (videos.id IS NOT NULL);
But you should use a foreign key for your connection between category and photo/video.
http://dev.mysql.com/doc/refman/5.1/de/innodb-foreign-key-constraints.html
Change the datatype of photos.category to (INT)
and the videos.category to (INT) it will be way faster when the table become larger
SELECT t1.category_name
FROM categories t1,
JOIN photos p ON p.category = t1.id
JOIN videos v ON v.category = t1.id
GROUP BY t1.category_name
ORDER BY t1.category_name ASC
You need to change the datatype of photos.category to INT instead of varchar and do the same for videos.category, then try below query.
SELECT Cat.category_name
FROM categories As Cat,
JOIN photos AS P ON P.category = cat.id JOIN videos AS vid ON vid.category = Cat.id
GROUP BY Cat.category_name
ORDER BY Cat.category_name ASC
thanks.

Categories