Search query with id - mysql

This are my tables:
Part table:
Part_id name
1 Case
2 Pen
3 Chicken
(Between table)table:
Part_id product_id
2 10
2 10
3 30
Product table:
product_id Name
10 Phone
20 Camera
30 Mouse
I want to search on the product name and get the product name back + the linked part name. But if i search on part name i want to get the part name and product name back.
I don't know how to join the tables but don't know how to preform an search like that.
Select part.name, product.name
from part
left join betweentable on part.part_id = betweentable.part_id
But i don't know how to join the next table. And than search to it.

Let me see if I understand this correctly... you have a name, which may be a product name or a part name, and you want to search for that, and return the linked parts if it's a product, or the linked products if it's a part.
If that's what you're asking, you can use the following query:
Select part.name, product.name
from part
left join betweentable on part.part_id = betweentable.part_id
left join product on product.id = betweentable.product_id
where part.name = <input_name>
union
Select part.name, product.name
from product
left join betweentable on product.id = betweentable.product_id
left join part on part.part_id = betweentable.part_id
where product.name = <input_name>;
< input_name > is the name you're searching for.

Simply add an other join:
Select part.name, product.name
from part
left join betweentable on part.part_id = betweentable.part_id
left join product on product.id = betweentable.product_id

Related

Multiple pictures per product in sql

product
-----------
product_id
product_name
product_description
price
size
productpictures
--------------
product_id
picture_id
pictures
---------
picture_id
picture_name
every product has 2 pictures but i only need the name, size, description and price once but i need both the pictures.
i used:
SELECT
product.product_description,
product.product_name,
product.price,
product.size,
pictures.picture_name
FROM product
LEFT OUTER JOIN productpictures
ON product.product_id = productpictures.product_id
LEFT OUTER JOIN bilde
ON productpictures.picture_id = pictures.picture_id
WHERE product.product_id = 1
But I get two rows with double up name, price, size and description, but the picture names are different, but how can i insert both picture names into my php file so it shows the information about the product and both pictures?
i tried printing the row picture name but that only gave me the first name but not the second.
size product_name price picture_name
50x70cm Sandvasket silke 399 Elfenben.PNG
50x70cm Sandvasket silke 399 SVElfenben.JPG
this is my current ressult with my current sql statement. basically whan i want in php is to echo the size name and price in a p tag, thats no problem but i also want to echo both the picture_names in a img tag
With just exactly two pictures per product, one solution is to use aggregation:
SELECT
pr.product_description,
pr.product_name,
pr.price,
pr.size,
MAX(pi.picture_name) picture_name1,
MIN(pi.picture_name) picture_name2
FROM product pr
LEFT JOIN productpictures pp ON pr.product_id = pp.product_id
LEFT JOIN pictures pi ON pp.picture_id = pi.picture_id
WHERE pr.product_id = 1
GROUP BY
pr.product_id,
pr.product_description,
pr.product_name,
pr.price,
pr.size
PS: I added table aliases to your query, they make the query easier to read and maintain (and allow self-JOINing when necessary...).

Join Four Tables

I looked and tried various examples, but still not get the results I need.
With have a table called "item" which has metadata about media (books, vidoes, mp3 ,etc"
and a table "item_format" with the URL of the video
and Table with "categories"
and a relational to called "categories_item"
I want to find all the record on our youTube videos sorted by categories. so that if you look for "parenting" we get list a list a youTube and the title.
parenting https://youtube/v/K8cJKirMo-A Love is The Tey
https://youtube/v/uodqINpEC_w Discipline
https://youtube/v/Ko-ZboCzR64 Become Her Friend
and so on, listing all the categories assigned
item.media-type LIKE '%video%'
item
===============
item_id media_type title
1 video/teachings Discipline
2 video/news December Update
3 video/landscape Quad Copter Noni Field
item_format
=================
item_format_id item_id format
1 2 https://youtube/v/K8cJKirMo-A
2 4 https://youtube/v/uodqINpEC_w
category
=================
category_id item_id name
1 2 parenting
2 4 ethics
category_item # the bridging table between item/category
=================
category_item_id category_id item_id
1 2 2
2 4 3
I tried things like this but get errors, frankly out my depth here
select item.title, item_format.format
from item i
left join item_format if
on i.item_id = if.item_id
left join category_item ci
on i.item_id = ci.item_id
left join category c
on ci.category_id = category_id
where i.media_type LIKE '%video5'
order by c.name
I can't get to first base
Unknown column 'item.title' in 'field list' #line 1
your query is ok , but a small change need. you use table alias. in first line just use table alias instead of table name
select i.title, if.format,c.name
from item i
left join item_format if on (i.item_id = if.item_id)
left join category_item ci on (i.item_id = ci.item_id)
left join category c on (ci.category_id = c.category_id)
where i.media_type LIKE '%video5'
order by c.name
you have to provide alias once you have given it:
see if it works:
select i.title, if.format,c.name
from item i
inner join item_format if
on i.item_id = if.item_id
inner join category c
on i.item_id=c.item_id
inner join category_item ci
on c.category_id=ci.category_id
where i.media_type LIKE '%video%'
order by c.name
or this:
select i.title,if.format,c.name
from item i,item_format if,category c, category_item ci
where
i.item_id=if.item_id and
i.item_id=c.item_id and
c.category_id=ci.category_id
order by c.name;

MYSQL Join if available

I have two tables I want to join, products and campaign_products
The tables look like this.
TABLE product
id | name
TABLE campaign_products
id | fk_campaign | fk_product | note
and if it's of interest
TABLE campaign
id | name
In all cases I need all the products returned, and if there is a row in the campaign_products which matches then add the values of those columns in as well. (null otherwise).
Remembering that multiple products can be part of multiple campaigns.
Here is what I tried, and in this case it only returns products that have a reference in campaign_products or products that don't exist at all in campaign_products so if the product is in campaign_products for another campaign it won't show up in other campaigns that don't include that product.
SELECT
product.id productId,
product.name productName,
campaign_product.note
campaignProductNote
FROMproduct
LEFT JOIN campaign_products ON product.id = campaign_products.fk_product
WHERE campaign_products.fk_campaign = 2 OR campaign_products.fk_campaign IS NULL
"2" changes based on the campaign being looked up.
I can't think of a way to solve this problem, is there a simple solution I'm missing ?
All you need to do is move the condition in the WHERE clause into the ON clause like so:
SELECT
product.id productId,
product.name productName,
campaign_product.note
campaignProductNote
FROM product
LEFT JOIN campaign_products ON product.id = campaign_products.fk_product
AND (campaign_products.fk_campaign = 2 OR campaign_products.fk_campaign IS NULL)

Matching items in one table that don't match in a subset of a second table

Suppose I have a Product table, and a
id product
1 Apple
2 Bag
3 Cat
4 Ducati
and a Cart table
id user_id product_id
1 1 2
2 1 3
3 2 1
4 3 1
So, I want to look at a particular user and see what he/she does NOT have in their Cart.
In other words, in the above example
SELECT ...... WHERE user_id=1 .....
would return Apple and Ducati because User 1 already has Bag and Cat.
(This may well duplicate another question but there are so many variations I couldn't find the exact match and put in these simple terms may help)
Perform a left join from product to all products purchased by user1, which can be retrieved with a subselect in the join. This will cause all product id's that are not in user1's care to have null product ids. The where clause will select all null product id's meaning they will not have been in a users cart, essentially filtering purchased items.
select p.name
from product p
left join (select product_id, user_id
from cart where user_id = 1)
c
on p.id = c.product_id
where c.product_id is null;
SQL Fiddle: http://sqlfiddle.com/#!2/5318eb/17
Select
*
From Product p
Where p.id Not In
(
Select c.product_id
From Cart c
Where User ID = ____
)
SELECT product FROM product_table
WHERE product NOT IN
(SELECT product_id FROM cart_table WHERE user_id = 1);
This will give you all product for all users which are not in there cart.
select c.user_id,a.Product
from cart c Cross Join product a
left Join
cart b on b.product_id=a.id and c.user_id=b.user_Id
where b.product_id is null
group by c.user_id,a.Product
Sql Fiddle Demo

Retrieve parent category name from table in MYSQL results

I have a MYSQL table called 'categories' from a project I inherited from someone else.
id parent_id name
1 NULL Travel
2 NULL Sleep
3 NULL Eat
4 NULL Bath
5 1 Prams
6 1 Travel Systems
7 2 Cots
8 3 High Chairs
The table is obviously a lot bigger than that, but you get the general idea. I have a MYSQL statement which brings together this table with other category, brand and product tables, but basically I want to list the parent category name from the above table with the sub-category in the statement. How do I do this?
My current statement is something like:
SELECT brands.name, products.name, categories.id, categories.name, brands.id,
FROM `products` , `brands` , `categories`
WHERE products.brand_id = brands.id
AND products.category_id = categories.id
AND brands.name = '$brand'
ORDER BY categories.name, products.name
How do I retrieve the parent category names in the results?
For example if the product is a Pram, how can I output "Travel". I could do seperate MYSQL statements in the loop but I want to avoid this. This is either a stupidly simple question (in which case I apologise for being brain dead) or a little more complicated! Thanks.
First you need to know the parent id of the current category and then get the name for that id, you could use a subquery in this way:
SELECT name FROM categories WHERE id = (SELECT pid FROM categories WHERE name = $brand)
EDIT: Since you need to get the category and subcategory names in the same row for a given subcategory id, try this:
SELECT sc.name AS subcategory, c.name AS category
FROM categories sc
LEFT JOIN categories c ON c.id = sc.parent
WHERE sc.id = $subcategory_id