How do i join all three tables? I have no idea, because i need to call them all into 1 table
customers
+--------+------------+---------------+---------+---------+
| serial | name | email | address | phone |
+--------+------------+---------------+---------+---------+
| 1 | first_name | email#web.com | address | 7777777 |
+--------+------------+---------------+---------+---------+
orders
+--------+------------+------------+
| serial | date | customerid |
+--------+------------+------------+
| 1 | 2014-03-04 | 1 |
+--------+------------+------------+
order_detail
+---------+-----------+----------+-------+
| orderid | productid | quantity | price |
+---------+-----------+----------+-------+
| 1 | 1 | 30 | 400 |
| 1 | 2 | 10 | 500 |
+---------+-----------+----------+-------+
customerid on table orders are the serial on table customers and orderid on table order_detail are the serial on orders
and what if i use another table? for the productid, where productid = product_id in another table?
help would be much appreciated, I am really sorry for the table, i have no idea how to make tables here but they are in order.
You simply need to use Join statements. So you can do following
Select * from Customers
join orders on Customers.serial = orders.customerid
join order_detail on orders.serial = order_detail.orderid
You can also create another table called Product and join the same way as i have showed you.
If you want to select columns from different tables then you have to do like using tablename.columnname. So for example if you want to select quantity and price from order_detail table then do following
Select * from Customers,order_detail.quantity,order_detail.price
join orders on Customers.serial = orders.customerid
join order_detail on orders.serial = order_detail.orderid
Hope you got my point.
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 have 2 tables let's say orders
id | memberid | productsid |
----------------------------
1 | 23 | 25,27
and products
id | product_name | price |
----------------------------
25 | product1 | 120
27 | product2 | 50
I want to join orders and products table to get product name and price for each id from productsid.
This is the way how I tried to store an order for a member. If you have any better solution, I am waiting to know.
You can join the tables with the help of the function find_in_set(), then group by each order and with group_concat() create a list of the products:
select o.id, o.memberid,
group_concat(p.product_name order by find_in_set(p.id, o.productsid)) products
from orders o inner join products p
on find_in_set(p.id, o.productsid)
group by o.id, o.memberid
See the demo.
Results:
| id | memberid | products |
| --- | -------- | ----------------- |
| 1 | 23 | product1,product2 |
I have two tables in the DB
tbl1: Items
Items tbl : id | item_name | item_price | item_qty
Items tbl Row 1: 1 | laptop | 100 | 10
Items tbl Row 2: 1 | television | 80 | 10
Items tbl Row 3: 1 | mobile | 60 | 10
tbl2: Orders
Orders tbl : id | order_num | items_id | items_prices | items+qty | amount
Orders tbl Row1: 1 | OR222879 | ["1,3"] | ["100,60"] | ["10,6"] | 1360;
I can't make a query returns Order's Items Data I'm using Codeigniter.
The more easier and effective solution would be to normalise your data and have each Item ID as separate rows in a different table.
Items - Orders - OrderItems
Orders - id | order_num | total
OrderItems - id | order_num | item_id | quantity
Using this structure you can select all rows using the 'order_num' from the OrderItems table.
What is the way to sort a MYSQL result from multiple tables?
I have two tables. The first:
"store_products" table:
+----+-----------+
| id | name |
+----+-----------+
| 1 | Product 1 |
| 2 | Product 2 |
| 3 | Product 3 |
+----+-----------+
Here i placed product names. Other table contains prices for different product variants:
"store_products_variants" table:
+-----+------------+-------------+-------------+
| id | product_id | price_sale | ordering |
+-----+------------+-------------+-------------+
| 5 | 1 | 06.00 | 2 |
| 6 | 1 | 32.00 | 3 |
| 11 | 1 | 56.00 | 1 |
| 14 | 2 | 09.00 | 1 |
| 44 | 3 | 15.00 | 1 |
+-----+------------+-------------+-------------+
I need to create a sort on price (lowest and highest), that uses only first variant - ordered by column "ordering" from "store_products_variants" table.
From example above, the results should be:
+---+------------+---------------+
| 1 | Product 2 | (price 09.00) |
| 3 | Product 3 | (price 15.00) |
| 2 | Product 1 | (price 56.00) |
+---+------------+---------------+
Is this possible in MySQL?
Use the ordering column to join the correct variant onto the product.
This would be the query if the correct ordering value was always the 1.
SELECT
products.name,
variants.price_sale
FROM store_products AS products
INNER JOIN store_products_variants AS variants
ON variants.product_id = products.id
AND variants.ordering = 1
ORDER BY variants.price_sale ASC
This query will first look for the lowest ordering value of a product. Then use it to join the price on your result:
SELECT
products.name,
variants.price_sale
FROM
store_products AS products
INNER JOIN (
SELECT
product_id,
MIN(ordering) AS ordering
FROM
store_products_variants
GROUP BY
product_id
) AS variantOrdering
ON variantOrdering.product_id = products.id
INNER JOIN store_products_variants AS variants
ON variants.product_id = variantOrdering.product_id
AND variants.ordering = variantOrdering.ordering
ORDER BY
variants.price_sale ASC
select t.* from(
select t1.[id], t1.[name],
'(price ' + cast(max(t2.[price_sale]) as varchar(50)) + ')' as [price]
from [#store_products] t1
left join [#store_products_variants] t2
on t1.[id] = t2.[]product_id
group by t1.[id], t1.[name]
)t
Order by len(t.[price]), t.[price];
Yes this is possible. Try to use the JOIN command, and JOIN on the ID of each table.
Hello this is possible please use this query
SELECT p.id,p.name,spv.price_sale
from products p
INNER JOIN store_products_variants spv ON spv.product_id = p.id
where spv.ordering = 1
group by p.id
order by spv.price_sale asc
i think it gives the result what you want
This topic has been much discussed but I was unable to find a solution that I can modify and make it work for my case. So maybe a more advanced expert will be able to help out.
I have a table called keywords which contains about 3000 rows with distinct keywords. Against each keyword there is a matching product_id, which are NOT unique, i.e. some of them are repeated. Table looks something like this:
+---------+------------+
| keyword | product_id |
+---------+------------+
| apple1 | 15 |
| apple2 | 15 |
| pear | 205 |
| cherry | 307 |
| melon | 5023 |
+---------+------------+
I have a second table called inventory that contains about 500K of products each with it's own product ID and other product data.
Now I need to get one random product row from inventory table that matches each product_id from keywords table and insert those rows into another table.
Resulting table should be something like this:
+---------+------------+---------+---------+---------+
| keyword | product_id | product | data1 | data2 |
+---------+------------+---------+---------+---------+
| apple1 | 15 | app5 | d1 | d2 |
| apple2 | 15 | app1 | d1 | d2 |
| pear | 205 | pear53 | d1 | d2 |
| cherry | 307 | cher74 | d1 | d2 |
| melon | 5023 | melo2 | d1 | d2 |
+---------+------------+---------+---------+---------+
This is my query at the moment and the problem is how to get a random product from inventory that matches a product_id:
SELECT keywords.keyword, keywords.product_id, inventory.*
FROM keywords LEFT OUTER JOIN
inventory
ON keywords.product_id = inventory.id
ORDER BY RAND();
If you want it to only return rows when there is a match between the tables, then you want a regular (i.e. inner) join not a left outer join. You can also add the word distinct.
SELECT DISTINCT keywords.keyword, keywords.product_id, inventory.*
FROM keywords JOIN
inventory
ON keywords.product_id = inventory.id
ORDER BY RAND();
And if you only want 1 row returned, add limit 1 at the end.
SELECT keywords.keyword, keywords.product_id, inventory.*
FROM keywords JOIN
inventory
ON keywords.product_id = inventory.id
ORDER BY RAND() LIMIT 1;
Is this what you want?
SELECT *
FROM (
SELECT keywords.keyword, keywords.product_id, inventory.*
FROM keywords JOIN
inventory
ON keywords.product_id = inventory.id
ORDER BY RAND()
) tmp
GROUP BY tmp.keyword;
I also test it at http://sqlfiddle.com/#!2/e559a9/2/0. Just run some times, the result will be randomize.