Select with INNER JOIN using PostgreSQL and PDO - php

I'm trying to make a SELECT with INNER JOIN using two tables that share the same related field name but no success.
I have to get from the SELECT the values of the field named taxes where the field type from the table products are the same on the table taxes.
What i have at the moment that doesn't work:
$stmt = $db->prepare("SELECT taxes
FROM taxes
INNER JOIN products
ON products.type = taxes.type");
$stmt->execute();

Did you try left join like this?
SELECT t.taxes FROM taxes t Left JOIN products p ON p.type = t.type;

Related

I want to use JOIN for a query and I want to show only the name of the product with the image

I'm practicing with queries and I have a question about JOIN.
I have these 2 tables:
**PRODUCTS**:
id
name
description
**PRODUCT_IMAGES**
id
product_id
image_link
And I want to use JOIN to combine these 2 table where I only want to see the name of the product with the specific image that is linked with the id.
I try to to that with this QUERY:
SELECT * FROM `product_images`
LEFT JOIN `products`
ON `product_images`.`products.id` = `products`.`id`;
The problem now is that I get all the information, but I only want to see the name and the image.
You have an error with product_images.id as it should be like "product_images.product_id" , I fixed this, kindly use this query.
SELECT products.name, product_images.image_link FROM `product_images`
INNER JOIN `products`
ON `product_images`.`product_id` = `products`.`id`;
Change * to name, image_link:
SELECT name, image_link FROM `product_images`
LEFT JOIN `products`
ON `product_images`.`products.id` = `products`.`id`;
SELECT products.id,products.name FROM `product_images`
LEFT JOIN `products`
ON `product_images`.`products.id` = `products`.`id`;
Instead of * you can give the column names comma separated.
You may choose LEFT JOIN or INNER JOIN as per your need.
Select the desired columns in the select statement. It's a best practice to name the table and column together. Otherwise if a column exists with the same name in both tables it will throw an error.
SELECT `products`.`name`,`product_images`.`image_link`
FROM `product_images`
LEFT JOIN `products`
ON `product_images`.`products.id` = `products`.`id`;
Simple use inner join
SELECT
p.name,
i.image_link
FROM
products p
JOIN
product_images i USING(product_id)
If you are sure, you have data in both tables then use the below
SELECT P.name, PI.image_link FROM product_images PI
inner JOIN products P
ON P.id = PI.product_id
If you are sure you have data in product table but might be not in product_image then use below
SELECT P.name, PI.image_link FROM products P
left JOIN product_images PI
ON P.id = PI.product_id

MYSQL Query Calculation in relationship

MySQL Schema
product_categories:
id, name
products
id,category_id,name,description
product_offers
id,product_id,name,email
product_offer_negotiations
id,product_offer_id,offer(float),by_user(ENUM-Seller/buyer),status
This is there query i have tried...
select products.*,
product_categories.name as category_name,
COUNT(product_offers.id) AS total_offers,
ROUND( AVG(product_offer_negotiations.offer), 2) AS avg_offer
from products
inner join product_categories on product_categories.id = products.category_id
inner join product_offers on product_offers.product_id = products.id
inner join product_offer_negotiations on product_offer_negotiations.product_offer_id = product_offers.id
and product_offer_negotiations.by_user = ?
group by products.id;
I would like to get all fields from product table, total_offers and avg_offer offer for product (only highest value from negotiation table per offer_id - no duplicatation)
This query only returns first row of products table. Thanks for help.

SELECT data from 4 tables in MySQL

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;

MySQL inner join with two tables

I'm very new to MySQL and PHP and I'm struggling with inner joins between two tables. I'm constructing a script that reads an os commerce database and tells me which products is currently on back order. In order for the product to be on back order a value in the products_attributes table is set to '134', however it only reads the product_id and not the product_model which is in the 'products' table.
products_attributes(table name)
options_values_id
product_id
products(table name)
product_id
product_model
I want to select items that have the value of '134' in the products_attributes table then match the product_ids from both tables to get the product_model from the "products" table. I feel like the code is very easy but for some reason I'm struggling with how to construct the query and to display it.
SELECT product_model FROM products as p,products_attributes as pa WHERE p.product_id = pa.product_id and pa.options_values_id = 134
or
SELECT p.product_model FROM products p INNER JOIN products_attributes as pa ON (p.product_id = pa.product_id) WHERE pa.options_values_id = 134
I think that you're asking how to get the product ID of anything have an attribute of 134
In that case, no JOIN is required.
SELECT DISTINCT
product_id
FROM
products_attributes
WHERE
options_values_id = 134
If you want the product_model then you could form the JOIN like this
SELECT DISTINCT
p.[product_model]
FROM
products p
JOIN
product_attributes pa
ON pa.[product_id] = p.[product_id]
WHERE
pa.[options_values_id] = 134
So the JOIN stipulates how the two tables are related to each other. Once there is a valid JOIN you can use the joined table nearly anywhere in the query.
You can try the following:
select p.product_id, p.product_model
from products p
inner join products_attributes pa on pa.product_id = p.product_id
where pa.options_values_id = '134'

PHP MySQL item categories

I'm trying to display a list of items with their name and all categories they belong to.
My database structure is:
Items table with Id, Item_Name.
Categories table with Id, Category_Name
Items_Categories table with Id, Item_id, Category_id.
I'd like to display the results in a table with the Item Name, and a comma-delimited list of its Categories' Names. I'm not sure how to do this with a single query. Thanks in advance.
You need to use the GROUP_CONCAT function that is available in mysql:
select i.item_name, group_concat(c.category_name) as categories
from items i
inner join items_categories ic on i.id = ic.item_id
inner join categories c on ic.category_id = c.id
group by i.item_name
I won't write it for you, but you need to use group_concat() function (MySQL).
I must say I did not test this query, but it should be something similar to the following:
SELECT i.Id, i.Item_name, GROUP_CONCAT(c.Category_Name) AS category_list
FROM Items i
JOIN Items_categories ci ON ci.Item_id = i.Id
LEFT JOIN Categories c ON c.Id = ci.Id
GROUP BY i.Id
Note that you can use GROUP_CONCAT to create a comma seperated list.
SELECT i.Item_Name, GROUP_CONCAT(c.Category_Name) AS Category_List
FROM Items AS i
LEFT OUTER JOIN (
Items_Categories AS ic
INNER JOIN Categories AS c ON ic.Category_id = c.Id
) ON i.Id = ic.Item_id
GROUP BY i.Id;

Categories