MYSQL Query Calculation in relationship - php

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.

Related

how to join 2 tables in SQL

I need to join my variants table to my products table so that I am able to get the product_id from both tables, how can I put this into my existing query..?
I've tried to add it not working.
$query = query("
SELECT *
FROM products
FULL OUTER
JOIN variants WHERE product_id=" . escape_string($_GET['add']). " ");
It sounds like you are looking for an inner join, not a full outer join. You'll also want to specify the column you are joining both tables on:
$query = query("
SELECT *
FROM products
JOIN variants ON variants.product_id = products.product_id
WHERE products.product_id =" . escape_string($_GET['add']). " ");
*Note: I'm assuming product_id is a column on both the variants and products table. If, for example, the product_id of products is just id, you'll want to change the join condition to:
ON variants.product_id = products.id

Select with INNER JOIN using PostgreSQL and PDO

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;

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;

Select non registred values in MYSQL

I have two tables in Mysql, Products and Inventory...
Products
int id_product
varchar name
Inventory
id_inventory
product_id
existence
So when my product '3' is out of stock I delete the row in Inventory where product_id = 3
And now I need to get all values that I have deleted on Inventory cause I need to make a report of missing products...
How can I make this query?
I used SELECT i.* FROM inventory i, products p WHERE i.product_id!=p.id_product but it doesn't works..
Thank you
LEFT JOIN would do as you wish as the rows you are looking for would have no corresponding row in inventory for a row in products, and so that gives the query ...
SELECT
p.product_id
FROM product p
LEFT JOIN inventory i
ON i.product_id = p.product_id
WHERE i.product_id IS NULL
Alternatively you could simply just not delete the row from inventory when unavailable. Just then do something like
SELECT
p.product_id
FROM product p
INNER JOIN inventory i
ON i.product_id = p.product_id
AND i.quantityInStock = 0
Either way works
or
select p.* from product p
where not exists (select null from inventory i where i.product_id=p.id_product)

Help with a MySQL query

Given the following tables I would like to know how to write a query to return only the categories that have books in them and the number of books for each category. A book can be added in one or many categories. I'm using PHP and MySQL.
Here are my tables:
categories table
- id
- name
- permalink
books table
- id
- title
- author
- description
- price
books_categories table
- id
- book_id
- category_id
select c.id
,c.name
,count(*) as num_books
from categories c
join books_categories bc on(bc.category_id = c.id)
group
by c.id
,c.name;
Use LEFT JOIN if you also want the categories without books.
SELECT *, COUNT(*) AS count
FROM books_categories AS bc
LEFT JOIN categories AS c ON c.id = bc.category_id
GROUP BY c.id
You'll get a count column with the number of rows for each category. Categories with zero books (that is those with no entry in table books_categories) won't be returned.
SELECT categories.id,
COUNT(books_categories.id) AS number
FROM categories
LEFT JOIN books_categories ON books_categories.category_id = categories.id
GROUP BY categories.id
HAVING number > 0;

Categories