I have tables like below.
Table A
id | val_a
1 | a1
2 | a2
3 | a3
Table B
id | id_a| val_b | resource_type
1 | 2 | b1 | 1
2 | 2 | b2 | 2
3 | 3 | b3 | 3
4 | 3 | b4 | 3
Table Resource_A
id |r_val| id_b
1 | ra1 | 1
Table Resource_B
id |r_val| id_b
1 | rb1 | 2
Table Resource_C
id |r_val| id_b
1 | rc1 | 3
2 | rc2 | 4
If resource_type is 1 in Table B, then table B make relation with table Resource_A.
If resource_type is 2 in Table B, then table B make relation with table Resource_B.
If resource_type is 3 in Table B, then table B make relation with table Resource_C.
Required Output is:
id_b | id_a| val_b |val_a | resource_type| r_val
1 | 2 | b1 | a1 | 1 | ra1
2 | 2 | b2 | a2 | 2 | rb1
3 | 3 | b3 | a3 | 3 | rc1
4 | 3 | b4 | a3 | 3 | rc2
But what is the best way to get it without using loop in laravel?
How Can I achieve this by using Laravel 5.2 OR Laravel 4 OR PHP OR MYSQL Query?
Thanks Ahead.
You only need to join the tables together as I have done below. The tricky part here is bringing in the correct r_val from the three resource tables. We can do this by left joining to each resource table, and then using the following expression to grab the matching value:
COALESCE(t1.r_val, t2.r_val, t3.r_val) AS r_val
This will take the first non NULL value from a resource table, in order of left to right. Assuming that each ID from tableb only appears once, in one of the resource tables, the order of the terms inside COALESCE() should not matter.
SELECT tb.id AS id_b,
tb.id_a,
tb.val_b,
COALESCE(ta.val_a, 'NA') AS val_a,
tb.resource_type,
COALESCE(t1.r_val, t2.r_val, t3.r_val) AS r_val
FROM tableb tb
LEFT JOIN tablea ta
ON tb.id_a = ta.id
LEFT JOIN resource_a t1
ON tb.id = t1.id_b
LEFT JOIN resource_b t2
ON tb.id = t2.id_b
LEFT JOIN resource_c t3
ON tb.id = t3.id_b
Demo here:
SQLFiddle
Related
I am trying to calculate the req_qty and in_qty from table1 and table2.
I am trying to fetch the joined 3 tables data SUM(req_qty), SUM(in_qty). Issue come if req.qty not save in table2 it will not works. I am left joining the table by this sku's ids
I made here a very simple table for calculating the sum of the column. generally, the sum of the column is multiplying the total rows.
Please don't close the post and guild for other thread. for my doubt clear i made here a table which I can understand. please help.
table1
ID | sku_1 | req_qty | trans_id
----------------------------------
1 | 123 | 150 | 12345
2 | 142 | 200 | 256314
3 | 123 | 100 | 896523
table2
ID | sku_2 | in_qty | trans_key
-----------------------------------
1 | 142 | 50 | 002563
table3
ID | sku_code | sku_name
--------------------------
1 | 142 | ABC
2 | 123 | XYZ
Expected Output
ID | sku | sku_name | reqQty | inQty
------------------------------------
1 | 123 | XYZ | 250 | 0
2 | 142 | ABC | 200 | 50
Edit to select data when table1 and table2 are empty
SELECT table1.id, table3.sku_code as sku, table3.sku_name,
sum(table1.req_qty) as reqQty, sum(table2.in_qty) as inQty
FROM table3
LEFT JOIN table1 on table3.sku_code = table1.sku_1
LEFT JOIN table2 on table3.sku_code = table2.sku_2
GROUP BY table1.id, table3.sku_code, table3.sku_name
Explanation
You can see an explanation on how left join works here https://www.w3schools.com/sql/sql_join_left.asp#:~:text=The%20LEFT%20JOIN%20keyword%20returns,if%20there%20is%20no%20match.
But to explain this query quickly, we will select all data from table3, left join will find all records from left table (here table3) and mathcing records from right tables (here table2 and table 1).
I am trying to get the sum of multiple rows from 2 different tables, but somehow the result returns multiple rows.
I need to get the SUM of quotation_item_amount (group by quotation_id) and invoice_item_amount (group by invoice_id) and if I query unpaid quotation, I need to get WHERE SUM(invoice) < SUM(quotation)
So here's my sample table
table client_project_id
+-------------------+-----------+----------------------+
| client_project_id | client_id | client_project_title |
+-------------------+-----------+----------------------+
| 23 | 5 | Project 1 |
| 17 | 9 | Project 2 |
| 54 | 7 | Project 3 |
+-------------------+-----------+----------------------+
table quotation
+--------------+-------------------+------------------+
| quotation_id | client_project_id | quotation_number |
+--------------+-------------------+------------------+
| 1 | 23 | Q/01/2020/001 |
| 2 | 17 | Q/01/2020/002 |
| 3 | 54 | Q/01/2020/003 |
+--------------+-------------------+------------------+
table quotation_item
+-------------------+--------------+-----------------------+
| quotation_item_id | quotation_id | quotation_item_amount |
+-------------------+--------------+-----------------------+
| 1 | 1 | 500 |
| 2 | 1 | 700 |
| 3 | 1 | 600 |
| 4 | 2 | 200 |
| 5 | 2 | 150 |
| 6 | 3 | 900 |
+-------------------+--------------+-----------------------+
table invoice
+--------------+-------------------+------------------+
| invoice_id | client_project_id | invoice_number |
+--------------+-------------------+------------------+
| 1 | 23 | I/01/2020/001 |
| 2 | 17 | I/01/2020/002 |
| 3 | 54 | I/01/2020/003 |
+--------------+-------------------+------------------+
table invoice_item
+-------------------+--------------+-----------------------+
| invoice_item_id | invoice_id | invoice_item_amount |
+-------------------+--------------+-----------------------+
| 1 | 1 | 500 |
| 2 | 1 | 700 |
| 3 | 1 | 600 |
| 4 | 2 | 200 |
| 5 | 2 | 150 |
| 6 | 3 | 900 |
+-------------------+--------------+-----------------------+
The result that I need to obtain is:
SUM of quotation_item_amount and SUM of invoice_item_amount PER client_project_id
To query WHERE SUM(invoice) < SUM(quotation)
Here is my latest try at the query
SELECT
SUM(quotation_item.quotation_item_amount) as quot_amt,
SUM(invoice_item.invoice_item_amount) as inv_amt,
data_client_project.client_project_id,
data_client.client_name
FROM data_client_project a
LEFT JOIN quotation b ON a.client_project_id = b.client_project_id
LEFT JOIN data_client d ON a.client_id = d.client_id
LEFT JOIN invoice i ON a.client_project_id = i.client_project_id
JOIN (
SELECT quotation_id,
SUM(c.quotation_item_amount) as quot_amt
FROM quotation_item c
GROUP BY c.quotation_id
) quotitem
ON b.quotation_id = quotitem.quotation_id
JOIN (
SELECT invoice_id,
SUM(e.invoice_item_price) as inv_amt
FROM invoice_item e
GROUP BY e.invoice_id
) invitem
ON i.invoice_id = invitem.invoice_id
However, this results in multiple duplicate rows of the quotation_item_amount and invoice_item_amount.
Have tried using UNION / UNION ALL and several other queries which just do not work.
Thank you for all your suggestions.
It looks like you are trying to aggregate along two different dimensions at the same time. The solution is to pre-aggregate along each dimension:
SELECT *
FROM data_client_project cp LEFT JOIN
(SELECT q.client_project_id,
SUM(qi.quotation_item_amount * qi.quotation_item_qty) as quot_amt
FROM quotation q JOIN
quotation_item qi
ON qi.quotation_id = q.quotation_id
GROUP BY q.client_project_id
) q
USING (client_project_id) LEFT JOIN
(SELECT i.client_project_id,
SUM(invoice_item_price) as inv_amt
FROM invoice i JOIN
invoice_item ii
ON i.invoice_id = ii.invoice_id
GROUP BY i.client_project_id
) i
USING (client_project_id);
Two notes about your style.
First, you are using arbitrary letters for table aliases. This makes the query quite hard to follow and becomes quite awkward if you add new tables, remove tables, or rearrange the names. Use abbreviations for the tables. Much easier to follow.
Second, I don't really recommend SELECT * for such queries. But, you can avoid duplicated column by replacing ON with USING.
I may be missing something, but your table descriptions do not include a example for data_client or data_client_project Given your example, I expect your row expansion is coming from the first 3 joins.
Make sure that the below is giving you the list of data you want first, then try joining in the calculation:
SELECT *
FROM data_client_project a
LEFT JOIN quotation b ON a.client_project_id = b.client_project_id
LEFT JOIN data_client d ON a.client_id = d.client_id
LEFT JOIN invoice i ON a.client_project_id = i.client_project_id;
#you may want to append the above with a limit 100 for testing.
if you have duplicated rows form the main query then add distinct for obatin a only distinct rows
and andd the where conditio for filtering the result by quotitem.quot_amt < invitem.inv_amt
SELECT distinct a.*, b.*, d.*, i.*
FROM data_client_project a
LEFT JOIN quotation b ON a.client_project_id = b.client_project_id
LEFT JOIN data_client d ON a.client_id = d.client_id
LEFT JOIN invoice i ON a.client_project_id = i.client_project_id
JOIN (
SELECT quotation_id,
SUM(c.quotation_item_amount * c.quotation_item_qty) as quot_amt
FROM quotation_item c
GROUP BY c.quotation_id
) quotitem ON b.quotation_id = quotitem.quotation_id
JOIN (
SELECT invoice_id,
SUM(e.invoice_item_price) as inv_amt
FROM invoice_item e
GROUP BY e.invoice_id
) invitem ON i.invoice_id = invitem.invoice_id
WHERE quotitem.quot_amt < invitem.inv_amt
Hello I have a database structure like this and some data in it
id | parent_id | name
1 | 0 | Nissan
2 | 1 | 240SX
3 | 1 | 350z
4 | 0 | Toyota
5 | 4 | Camry
6 | 4 | Prado
7 | 1 | Skyline
8 | 4 | Hilux
I want to take Nissan as heading and after show all the models. As well Toyota as heading and it's models below it. How do I achieve this using one query? Is it even possible?
You should normalize your database or at least separate your columns so there is a manufacturer column and a model column. This will then help you do a query such as
SELECT * FROM CARS WHERE manufacturer = 'Toyota';
or
SELECT * FROM CARS GROUP BY manufacturer;
But the best practise would be to have a table for storing manufacturers and a table for storing all models of the cars, then a table for storing your cars itself which will have 2 columns that are referenced from the other 2 tables.
So this will become:
id | parent_id | manufacturer_id | model_id
1 | 0 | 1 | 2 |
2 | 1 | 4 | 1 |
3 | 1 | 3 | 6 |
4 | 0 | 3 | 9 |
you will then do a query to join the 3 tables
SELECT cars.id, cars.parent_id, cars.manufacturer_id, cars.model_id, t1.manufacturer_name, t2.model_name from CARS
JOIN manufacturer_table t1
ON t1.manufacturer_id = cars.manufacturer_id
JOIN model_table t2
ON t2.model_id = cars.model_id
hope that helps
Try this, it should solve your problem:
SELECT
child.*
FROM
{your_table} parent
LEFT JOIN
{your_table} child ON child.parent_id = parent.id
OR (child.parent_id = 0 AND child.id = parent.id)
WHERE
parent.parent_id = 0
ORDER BY
parent.id, child.parent_id, child.name
{your_table} should be replaced in two places with your table's name (which you presented in the question).
I have the following code where i need to select all items from personabisna table and count items with the same personalbisnaId from another table where the both tables share the personalbisnaid
$query="select c.BusinessLogo,
c.PersonalBisnaId,
c.account_id,
AS Ads from personalbisna As c INNER JOIN myads AS b on b.PersonalBisnaId=c.PersonalBisnaId GROUP BY c.PersonalBisnaId LIMIT $itemfrom,$dataperpage";
These are the tables
Personalbisna table
| PersonalBisnaId| account_id| BusinessLogo
---------------------------------------------
| 1 | 23 | qwertyu.jpg
| 2 | 4 | asdfghjk.jpg
| 3 | 12 | 34567gfd.jpg
| 4 | 34 | drtyujhv.jpg
myads table
| MyAdsId | PersonalBisnaId| AdType
---------------------------------------------
| 1 | 2 | logo
| 2 | 2 | business card
| 3 | 3 | logo
| 4 | 2 | caricalture
I have used some already answered questions to solve my problem and i'm really getting totally confused to solve my issue
The above query should output the following
| PersonalBisnaId| account_id| BusinessLogo | AdsCount
-------------------------------------------------------
| 1 | 23 | qwertyu.jpg | 0
| 2 | 4 | asdfghjk.jpg | 3
| 3 | 12 | 34567gfd.jpg | 1
| 4 | 34 | drtyujhv.jpg | 0
This what i have
$query="SELECT
c.BusinessLogo,
c.PersonalBisnaId,
c.account_id,
c.BusinessName,
c.BusinessCategory,
c.BusinessSubCategory,
c.town,
c.estate,
c.street,
c.road,
c.building,
c.Address,
c.city,
c.PhoneNumber,
c.AltPhoneNumber,
c.website,
c.Email,
c.BusinessType
COUNT(MyAdsId) AS AdsCount
FROM personalbisna AS c
LEFT OUTER JOIN myads AS b
ON b.PersonalBisnaId= c.PersonalBisnaId
GROUP BY c.PersonalBisnaId LIMIT $itemfrom,$dataperpage";
Count aggregate is missing in your query.
Also if you want select all items from Personabisna table then instead of INNER JOIN you need to Left/Right Outer Join
SELECT c.personalbisnaid,
c.account_id,
c.BusinessLogo,
Count(AdType) AS AdsCount
FROM personalbisna AS c
LEFT OUTER JOIN myads AS b
ON b.personalbisnaid=c.personalbisnaid
GROUP BY c.personalbisnaid,
c.account_id,
c.BusinessLogo
I have a query to write and I am absolutely stumped on how to do it. Here's my situation, I am trying to provide a particular product_ID, then match all of the other product_IDs in the database that have at least the same intDescription_detail_IDs as the provided product_ID.
The relevant tables look like this:
tblproducts
=========================
product_ID | product_name
=========================
| 1 | dresser |
| 2 | bookcase |
| 3 | table |
| 4 | chair |
=========================
tbldescriptions
=========================================================================
|description_ID| intDescription_product_ID | intDescription_detail_ID |
=========================================================================
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 4 | 2 | 1 |
| 5 | 2 | 2 |
| 6 | 2 | 6 |
| 7 | 3 | 1 |
| 8 | 3 | 3 |
| 9 | 3 | 4 |
| 10 | 4 | 1 |
| 11 | 4 | 2 |
| 12 | 4 | 7 |
As an example, if I provided the product_ID "1", then I would like to return all of the product_IDs that at least have intDescription_detail_ID 1 and 2.
So, the product_IDs that should be returned are 1, 2, and 4, because all of these products have the intDescription_detail_ID of 1 and 2 among their details.
I am highly confused about how to write a query like this, so any help is greatly appreciated!
I should warn you by saying that I may have made a silly mistake here...
DROP TABLE IF EXISTS products;
CREATE TABLE products(product_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,product_name VARCHAR(20) NOT NULL UNIQUE);
INSERT INTO products VALUES
(1,'dresser'),
(2,'bookcase'),
(3,'table'),
(4,'chair');
DROP TABLE IF EXISTS product_detail;
CREATE TABLE product_detail
(product_id INT NOT NULL
,detail_id INT NOT NULL
,PRIMARY KEY(product_id,detail_id)
);
INSERT INTO product_detail VALUES
(1,1),
(1,2),
(2,1),
(2,2),
(2,6),
(3,1),
(3,3),
(3,4),
(4,1),
(4,2),
(4,7);
SELECT DISTINCT c.product_id
FROM product_detail a
JOIN product_detail b
ON b.product_id = a.product_id
AND b.detail_id <> a.detail_id
JOIN product_detail c
ON c.product_id <> a.product_id
AND c.detail_id = a.detail_id
JOIN product_detail d
ON d.product_id = c.product_id
AND d.detail_id = b.detail_id
WHERE a.product_id = 1;
+------------+
| product_id |
+------------+
| 2 |
| 4 |
+------------+
Alternative to #Strawberry’s suggestion with JOINs this can also be done using HAVING for filtering products that have (at least) the same number of rows with the same intDescription_detail_IDs as the product the search is done for:
SELECT intDescription_product_ID
FROM tbldescriptions t1
WHERE intDescription_detail_ID IN (
SELECT intDescription_detail_ID
FROM tbldescriptions t2
WHERE t2.intDescription_product_ID = 1
)
GROUP BY intDescription_product_ID
HAVING count(*) >= (
SELECT count(intDescription_detail_ID)
FROM tbldescriptions t3
WHERE t3.intDescription_product_ID = 1
)
http://sqlfiddle.com/#!2/ce698/2
One should keep in mind though that HAVING is applied last, so that will select all products with at least one matching intDescription_detail_ID first, and filter the results based on the actual count afterwards – so depending on the size and characteristic of your data set that might not be the best performing solution.