Join self-join table with another table - mysql

I have two tables: products and meta.
Products table:
+----+----------+
| id | name |
+----+----------+
| 1 | TV |
| 2 | Computer |
| 3 | Freezer |
+----+----------+
Meta table:
+----+------------+-----------+------------+
| id | product_id | meta_key | meta_value |
+----+------------+-----------+------------+
| 1 | 1 | currency | USD |
| 2 | 1 | price | 1100 |
| 3 | 2 | currency | PLN |
| 4 | 2 | price | 9300 |
| 5 | 3 | currency | USD |
| 6 | 3 | price | 1200 |
+----+------------+-----------+------------+
now the following query works fine:
select price.product_id, products.name, price.meta_value as 'price', currency.meta_value as 'currency'
from meta as price
join meta as currency on(price.product_id=currency.product_id and currency.meta_key='currency')
join products on(products.id=price.product_id)
where price.meta_key='price';
result:
+------------+----------+-------+----------+
| product_id | name | price | currency |
+------------+----------+-------+----------+
| 1 | TV | 1100 | USD |
| 2 | Computer | 9300 | PLN |
| 3 | Freezer | 1200 | USD |
+------------+----------+-------+----------+
but the query:
select price.product_id, products.name, price.meta_value as 'price', currency.meta_value as 'currency'
from meta as price, meta as currency
join products on(products.id=price.product_id)
where
price.product_id=currency.product_id
and price.meta_key='price'
and currency.meta_key='currency';
returns: "Unknown column 'price.product_id' in 'on clause'"
Why does that happen ?

Your "from" clause is interpreted as:
from meta as price, (meta as currency join products on (products.id = price.product_id)
So, there is no price.product_id available to the on clause, as it only knows about the meta as currency and products tables.

Related

How to get count on a separte column?

I have the following table which I will like to get the count of items with the same product name on a separate column
Here is the table
+-----------+-------------+-------+------+
| ProductID | ProductName | Price | URL |
+-----------+-------------+-------+------+
| 1 | Book | 2 | url1 |
| 2 | Pen | 1 | url2 |
| 3 | pencil | 0.5 | url3 |
| 4 | Book | 2 | url1 |
+-----------+-------------+-------+------+
And I will like get the following from the table
+-----------+-------------+-------+------+-------+
| ProductID | ProductName | Price | URL | Count |
+-----------+-------------+-------+------+-------+
| 1 | Book | 2 | url1 | 2 |
| 2 | Pen | 1 | url2 | 1 |
| 3 | pencil | 0.5 | url3 | 1 |
+-----------+-------------+-------+------+-------+
The reason why I need this is because the items need to be rendered on an external application with the count. I do not know how to get the count on another column.
This looks like aggregation:
select min(ProductID) as ProductID, ProductName, Price, URL, COUNT(*)
from t
group by ProductName, Price, URL;
SELECT ProductID
,ProductName
,Price
,URL
,COUNT(ProductName) AS CountProduct
FROM products
GROUP BY ProductName

MySql get Count, Value from 3rd Table with reference from 1st table

i would like to get the Count value from the 3rd table... 1st table has reference id to 2nd table, 2nd table has reference id of 3rd table... in 3rd table has the value... that i need to count...
Table struct:
table1: tbl_rack
+------------+---------+--------+
| rack_id | site_id | status |
+------------+---------+--------+
| R-642 | ST5 | Y |
| R-307 | ST6 | Y |
| R-57 | ST7 | Y |
| 390/6 | ST8 | Y |
| 9706 | ST11 | Y |
table2: tbl_site
+---------+-------------+-----------+
| site_id | customer_id | region_id |
+---------+-------------+-----------+
| ST5 | CM8 | RM4 |
| ST6 | CM8 | RM8 |
| ST7 | CM10 | RM2 |
| ST8 | CM11 | RM12 |
| ST11 | CM8 | RM10 |
table3: tbl_customer
+-------------+----------------------+---------------+
| customer_id | customer_name | customer_type |
+-------------+----------------------+---------------+
| CM8 | LIVI-IN | MODERATE |
| CM10 | PEPE | HIGH |
| CM11 | SANDER | LOW |
| CM12 | TOASTER | MODERATE |
I want to count each customers contains how many Racks where ranks status is 'Y'
expected Result1:
Customer No.of Racks
LIVI-IN 3
OTHERS 2
expected Result2:
Customer Type No.of Racks
Moderate 3
High 1
Low 1
Please, follow below SQL query:
select C.customer_name as 'Customer', count(*) as 'No.of Racks'
from tbl_customer C
left outer join tbl_site TS on TS.customer_id = C.customer_id
left outer join tbl_rack TR on TR.site_id = TS.site_id
group by C.customer_name
order by C.customer_name

MYSQL Join returns duplicate column name

I am new to SQL and have the following challenge, I have a table on the left that has products assigned to a single company and denoted by a ref_id (it comes from another table, and I have simplified it for sake of question) and I have a master list. I want to compare all the products for company "A" against the master list and show all the products company A has not subscribed to. I am using JOIN and end up getting duplicate column names which throws my PHP program off. Here is what I have tried several syntax for UNION, RIGHT JOIN, LEFT JOIN and NOT operators, and the below is the closest I have gotten, however I am getting duplicate column names and I only need the last table with the last 2 columns. Please advise.
mysql> select ref_id, product_id, short_name from ap_company_product order by ref_id, product_id;
+--------+------------+------------+
| ref_id | product_id | short_name |
+--------+------------+------------+
|      2 | 10         | product 10 |
|      2 | 11         | product 11 |
|      2 | 12         | product 12 |
|      2 | 15         | product 15 |
|      2 | 17         | product 17 |
|      2 | 21         | product 21 |
|      3 | 11         | product 11 |
|      3 | 13         | product 13 |
|      3 | 17         | product 17 |
|      3 | 20         | product 20 |
+--------+------------+------------+
10 rows in set (0.00 sec)
THE MAIN LIST
mysql> select  product_id, short_name from ap_company_product_list;
+------------+-------------+
| product_id | short_name  |
+------------+-------------+
| 10         | product 10  |
| 11         | product 11  |
| 12         | product 12  |
| 13         | product 13  |
| 14         | product 14  |
| 15         | product 15  |
| 16         | product 16  |
| 17         | product 17  |
| 18         | product 18  |
| 19         | product 19  |
| 20         | product 20  |
| 21         | product 21  |
| 22         | product 22  |
+------------+-------------+
13 rows in set (0.00 sec)
Another SQL which is the actual results I need
SELECT aa.product_id as product_id, aa.short_name as short_name, aa.ref_id as ref_id, bb.product_id as product_id, bb.short_name as short_name FROM (select ref_id, product_id, short_name from ap_company_product where ref_id=2) aa RIGHT OUTER JOIN ap_company_product_list bb ON aa.product_id = bb.product_id where aa.product_id is NULL;
+------------+------------+--------+------------+------------+
| product_id | short_name | ref_id | product_id | short_name |
+------------+------------+--------+------------+------------+
| NULL       | NULL       |   NULL | 13         | product 13 |
| NULL       | NULL       |   NULL | 14         | product 14 |
| NULL       | NULL       |   NULL | 16         | product 16 |
| NULL       | NULL       |   NULL | 18         | product 18 |
| NULL       | NULL       |   NULL | 19         | product 19 |
| NULL       | NULL       |   NULL | 20         | product 20 |
| NULL       | NULL       |   NULL | 22         | product 22 |
+------------+------------+--------+------------+------------+
7 rows in set (0.01 sec)
Just use this:
SELECT bb.product_id as product_id, bb.short_name as short_name FROM (select ref_id, product_id, short_name from ap_company_product where ref_id=2) aa RIGHT OUTER JOIN ap_company_product_list bb ON aa.product_id = bb.product_id where aa.product_id is NULL;
BTW, I would prefer this one:
SELECT product_id, short_name FROM ap_company_product_list WHERE product_id NOT IN (SELECT product_id FROM ap_company_product);

MySQL join on three tables EAV model

Kindly consider the following tables:
invoices
+-----------+----+------------+--------+---------+
| accountid | id | customerid | total | balance |
+-----------+----+------------+--------+---------+
| 1 | 2 | 167909 | 120060 | 120060 |
+-----------+----+------------+--------+---------+
invoices_attributes
+-----------+----+--------------+
| accountid | id | name |
+-----------+----+--------------+
| 1 | 1 | registration |
+-----------+----+--------------+
| 1 | 2 | claimnumber |
+-----------+----+--------------+
| 1 | 3 | jobid |
+-----------+----+--------------+
invoices_attributes_values
+------------------+-------------+-----------+---------------+
| attributevalueid | attributeid | invoiceid | value |
+------------------+-------------+-----------+---------------+
| 1 | 1 | 2 | ABC 126L |
+------------------+-------------+-----------+---------------+
| 2 | 2 | 2 | ABZ123 |
+------------------+-------------+-----------+---------------+
| 3 | 3 | 2 | MARY DOE |
+------------------+-------------+-----------+---------------+
Through the help of Eugen Rieck's original answer I was able to make the following query
SELECT
invoices.accountid,
invoices.id AS invoiceid,
invoices.customerid,
invoices.total,
registration.value AS registration,
claimnumber.value AS claimnumber,
jobid.value as jobid
FROM
invoices
LEFT JOIN invoice_attributes ON invoices.accountid=invoice_attributes.accountid
LEFT JOIN invoice_attribute_values AS registration ON registration.attributeid = invoice_attributes.id AND invoices.id = registration.invoiceid AND invoice_attributes.name = 'registration'
LEFT JOIN invoice_attribute_values AS claimnumber ON claimnumber.attributeid = invoice_attributes.id AND invoices.id = claimnumber.invoiceid AND invoice_attributes.name = 'claimnumber'
LEFT JOIN invoice_attribute_values AS jobid ON jobid.attributeid = invoice_attributes.id AND invoices.id = jobid.invoiceid AND invoice_attributes.name = 'jobid'
Which gave the following result
+-----------+-----------+------------+--------+--------------+-------------+----------+
| accountid | invoiceid | customerid | total | registration | claimnumber | jobid |
+-----------+-----------+------------+--------+--------------+-------------+----------+
| 1 | 2 | 167909 | 120060 | NULL | NULL | MARY DOE |
+-----------+-----------+------------+--------+--------------+-------------+----------+
| 1 | 2 | 167909 | 120060 | NULL | ABZ123 | NULL |
+-----------+-----------+------------+--------+--------------+-------------+----------+
| 1 | 2 | 167909 | 120060 | ABC 126L | NULL | NULL |
+-----------+-----------+------------+--------+--------------+-------------+----------+
When I GROUP BY invoices.id some of the columns (registration, claimnumner or job) will become NULL. I desire the result to be as:
+-----------+-----------+------------+--------+--------------+-------------+----------+
| accountid | invoiceid | customerid | total | registration | claimnumber | jobid |
+-----------+-----------+------------+--------+--------------+-------------+----------+
| 1 | 2 | 167909 | 120060 | ABC 126L | ABZ123 | MARY DOE |
+-----------+-----------+------------+--------+--------------+-------------+----------+
How can the query be modified to get the result above?
SQL has no provision to make the columns dependant on the data. You could however create a query with ALL possible attributes along the lines of
Basically you want to renormalize an EVA structure - this is of course possible:
SELECT
invoices.accountid,
invoices.id AS invoiceid
invoices.customerid,
invoices.total,
jobids.value AS jobid -- one of these lines per attriubute
FROM
invoices
LEFT JOIN invoices_attributes ON invoices.accountid=invoices_attributes.accountid
-- One of the following joins per attribute
LEFT JOIN invoices_attributes_values AS jobids
ON jobids.attr_id=invoices_attributes.attr_id
AND jobids.accountid=invoices.accountid
AND jobids.invoiceid=invoices.id
AND invoices_attributes.attr_name='jobid'

MySQL combining outer joins

For this question I have created a simple example that illustrates what I am asking.
Say I had a table called 'books'
+----+----------------------------+-----------+
| pk | title | author_id |
+----+----------------------------+-----------+
| 1 | The Lost Symbol | 1 |
| 2 | Follow Us Home | 2 |
| 3 | The Man in the High Castle | 3 |
+----+----------------------------+-----------+
(table a)
And another table called 'shops', that had a list of shops that sold each book:
+----+---------+-------------+-------+
| pk | book_id | shop_name | price |
+----+---------+-------------+-------+
| 1 | 1 | WHSmith | 5.00 |
| 2 | 1 | Waterstones | 7.00 |
| 3 | 1 | Amazon | 2.50 |
| 4 | 2 | WHSmith | 4.00 |
| 5 | 2 | Borders | 4.50 |
+----+---------+-------------+-------+
(table b)
If I do a simple select that grabs a book and all of the places it is sold using a join such as:
SELECT
books.*,
shops.shop_name,
shops.price
FROM
books
JOIN shops ON books.pk = shops.book_id
WHERE
book.book_name = "The Lost Symbol"
I would get results such as below:
+----+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+----+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | WHSmith | 5.00 |
| 1 | The Lost Symbol | 1 | Waterstones | 7.00 |
| 1 | The Lost Symbol | 1 | Amazon | 2.50 |
+----+-----------------+-----------+-------------+-------+
(table c)
However, I would LIKE to receive results like this:
+----+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+----+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | NULL | NULL |
| 1 | The Lost Symbol | 1 | WHSmith | 5.00 |
| 1 | The Lost Symbol | 1 | Waterstones | 7.00 |
| 1 | The Lost Symbol | 1 | Amazon | 2.50 |
+----+-----------------+-----------+-------------+-------+
(table d)
I.e. the first row is just the result of left outer join and the rest of the results are the the inner join.
An even more desired outcome is:
+------+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+------+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | NULL | NULL |
| NULL | NULL | NULL | WHSmith | 5.00 |
| NULL | NULL | NULL | Waterstones | 7.00 |
| NULL | NULL | NULL | Amazon | 2.50 |
+------+-----------------+-----------+-------------+-------+
(table e)
Having shop_name and price concatenated and grouped in a single row seems not to work as it only does the first result from shops instead of all of them, also in my real world scenario, I have punctuation in the data so have to be careful with the separator.
So how would I get the result of table e?
You can use UNION ALL to build the required result set:
SELECT pk, title, author_id, NULL AS shop_name, NULL AS price
FROM books
WHERE books.title = "The Lost Symbol"
UNION ALL
SELECT NULL AS pk, NULL AS title, NULL AS author_id, shops.shop_name, shops.price
FROM books
JOIN shops ON books.pk = shops.book_id
WHERE books.title = "The Lost Symbol"
The first part of the union operation returns the first row of the result, i.e. the book title. The second part returns the rest of the rows, i.e.the shop names.
Demo here