Complete table with another table which has same id - mysql

i have two table "product" and "sales". product have "id,Name,price" and sales have "id,pro_id" .
in product table save all products with unique id, and in sales table save all sales product with id of product in "pro_id".
i want a sql which can report a table sales with full data of product info.
for example report have "pri_id,Name,Price" columns.
sample:
product:
id--------Name------price 1---------RAM-------300
2---------CPU-------400
sales:
id---------pro_id 1---------2 2---------1 3---------2
4---------2 5---------1
result of report :
pro_id----Name------price 2---------CPU-------400
1---------RAM-------300 2---------CPU-------400
2---------CPU-------400 1---------RAM-------300

SELECT pro_id,name,price FROM product JOIN sales ON sales.pro_id =
product.id

Related

I want to Create a receipt – order number, customer, list of items, prices and date. in MySQL

I want output in such a way that for customer Jim Wilson, the product name should be in the same box.

SubTotal Query based on condition of other column

I am creating a spare part management database in Microsoft Access. I have two table which are ItemTable and EntryTable. ItemTable holds information about each item with unique ItemID and EntryTable holds information of each items usage. I need to calculate the total stock left for each items based on the usage.
So as you can see, for the ItemID with 2, i need to calculate the total stock left based on the usage of In or Out of Status field.
If status is In then plus elseif status is Out then minus. Then total the stock of ItemID 2. Thus the total stock for ItemID 2 will be 3. I have figured out by total and group by for the ItemID but i cannot figure out the way to subtotal based on condition from other column. Thank you.
You can do it with conditional aggregation:
select itemid,
sum(iif(status = 'In', 1, -1) * quantity) as total
from entrytable
group by itemid

How to join tables correctly. 0 rows returned is not correct

Everything looks in order for the code to run, but the code isn't showing any rows. So, i must be doing something wrong. I can't figure out where the problem is. The problem from my instructor is:
Use the AP database
Vendor name from the vendor table
Vendor Contact First Name and Vendor Contact Last Name from the vendor table but I need them concatenated together with a space between the names
Invoice Date from the invoice table
Only the day of from the Invoice Date
Invoice Number from the invoice table
The third and fourth characters from the Invoice Number
Line Item Amount from the invoice line items table
Calculate the commission which is the Line Item Amount * 15% and make it ONLY two decimals
The FROM is the Vendors table do JOINS on the others
The WHERE is where the Line Item Amount is greater than $2000 and the Invoice Date is between July 1, 2014 and July 31st, 2014 and I only want the ones that have either a 9 or a 2 in the Invoice Number
Sort by Invoice Date
// This is the only information he's given me for this assignment. ^
I've tried to join different columns together in different orders/joining tables in different orders.
select vendor_name as vendor, concat(first_name, ' ', last_name) as Contact,
DAY(invoice_date) as ofday, invoice_date,
SUBSTRING(invoice_number, 3,2) as thirdFourth,
line_item_amount,round(line_item_amount*.15, 2) as Commission
from vendors v
JOIN vendor_contacts vc
ON v.vendor_id = vc.vendor_id
JOIN INVOICES INV
ON VC.VENDOR_ID = INV.VENDOR_ID
JOIN invoice_line_items ILI
ON INV.invoice_id = ILI.invoice_id
where line_item_amount > 2000
and invoice_date between '2014-7-1' and '2014-7-31'
and (invoice_number =9 OR invoice_number =2)
expected results :
https://imgur.com/E81ix3o
At the moment you are asking specifically for invoice_number 9 or 2. You don't have an invoice_number of 9 or 2 If you want invoice_numbers with 9 or 2 in you will need
invoice_number like '%9%' or invoice_number like '%2%'

sorting mixed table datas

I have two tables in a MySQL database. First table is contacts(customer, id) that stores customers' information. Second table history(report, nextFollowingDate, customerid) store the history of a customer contact, along with next following date. A customer can have multiple records with different values for nextFollowingDate.
Sample data are as follows.
contacts table:
customer id
a 1
b 2
c 3
history table:
report nextFollowingDate customerid
report1 2018/04/23 1
report2 2018/04/25 1
report3 2018/04/22 2
report4 2018/04/26 3
report5 2018/05/30 2
I would like to sort customers in contacts table with on the values of nextFollowingDate in the ascending order. It would look like follows.
customer nextFollow
1 2018/04/25
2 2018/05/30
3 2018/04/26
But I have no way in my mind of doing that.
SELECT customerId, MAX(nextFollowingDate) FROM history GROUP BY customerId
is what you are looking for. However, what do you exactly mean by last following date ASC is not very clear. I have written this answer based on your example result set that you have given as your expectation. Find this SQL Fiddle to see this in action.

How to store product quantity in database

I have database with three tables
product (productID,productName,productDescription)
size (sizeID , sizeName) eg. s ,m ,l ,8,10,12,37,35..etc
ProductDetails (ProductDetailsID, productID(FK) , sizeID (FK))
I want to store the quantity of each product and it size for example there are 5 in stock for product 1 of size s, 10 in stock for product 4 size 37 ..etc
In which table should I place the quantity ?
You can store it in ProductDetails only as the quantity is dependent upon the product and its size both.
You should use another table.
It could be called ProductSizeQuantity(id, ProductId, sizeID, Quantity)
you have
product (productID,productName,productDescription)
size (sizeID , sizeName)
ProductDetails (ProductDetailsID, productID(FK) , sizeID (FK))
in my opinion you need:
product (productID[PK], productName, productDescription)
size (size[PK])
ProductDetails (productID[PK] , size[PK], quantity)
notes:
for size it is not required sizeid + sizeName, but only one column with size name
quantity is in product details table, in this table id your PK is productId+Size you'll have one tuple for each for each size of the product
You can do something like this.
Product table :
id
name
description
Size table :
id
product_id
size
Quantity table :
id
size_id
stock
Fetch product first, fetch sizes based on product and fetch its Quantity based on size id.