I have two tables invoice and invoice_product
In the table invoice_product there is a column serialno
How to search for invoices that contain a product with a specific serial number?
Although difficult to provide a full working example, replacing the fields in the following query with what you need from invoices should do the trick:
SELECT i.id
FROM invoice i
INNER JOIN invoice_product ip ON ip.invoice_id = i.id
WHERE ip.serialno = 'YOUR_NUMBER'
GROUP BY i.id
Here you should have the invoiceno column in both invoice and invoice_product tables. Then you can combine two tables using invoiceno column.
you can use this following code to get that specific Invoices according to the product serial no
SELECT invoice.invoiceno FROM invoice
INNER JOIN invoice_product ON invoice.invoiceno=invoice_product.invoiceno
WHERE invoice_product.serialno='12345'
Related
I believe I have formed this question title correctly because I wasn't sure how to form it. As an example, I have summarized my query below.
I have an order table which saves order details like customer id, address and product ids and quantity ordered for each order in a row. So multiple inventory/product ids are saved in a single row.
so my query looks like: this is a summarized query for an easier explanation I have omitted various other fields.
SELECT customer.name,customer.address,tbl_order.order_date,tbl_order.product1_id,tbl_order.product2_id,inventory.product1_name,inventory.product2_name
FROM tbl_order
INNER JOIN customer ON tbl_order.customer_id = customer.id
INNER JOIN inventory on tbl_order.product1_id = inventory.id
INNER JOIN inventory on tbl_order.product2_id = inventory.id
where YEAR(tbl_order.order_date)='$year'
So my question is how to get the inventory details from the inventory table based on each product id from tbl_order. I am running a while loop to show all data for a year
while($row=mysqli_fetch_assoc($sql1))
I can divide this query into 2 and run the inventory query individually but then how to combine the while loop, as sometimes there could also be empty query when some products are not in order table (depending on order to order, not all products are ordered) so this doesn't work
while($row=mysqli_fetch_assoc($sql1)) and ($row1=mysqli_fetch_assoc($inv1)) and ($row2=mysqli_fetch_assoc($inv2))
and so one for 10 products
First, of all you have bad DB design and I kindly advice to normalize your DB.
Second, if you can not re-design the DB you can use multiple joins with aliases like:
SELECT
customer.name, customer.address, tbl_order.order_date,
tbl_order.product1_id, inv1.product1_name,
tbl_order.product2_id, inv2.product2_name
FROM tbl_order
INNER JOIN customer ON tbl_order.customer_id = customer.id
INNER JOIN inventory AS inv1 ON tbl_order.product1_id = inv1.id
INNER JOIN inventory AS inv2 ON tbl_order.product2_id = inv2.id
WHERE YEAR(tbl_order.order_date)='$year'
I have these two tables:
invoices (contains id field)
contracts (contains fk to invoice + 'code' field)
Let's say I have one record in the invoices table and two records in the contracts table. Both records in the contracts table point to the same invoice record.
Desire: I'd like to count the amount of invoices.
What I've got so far:
select
c.code, count(*)
from
invoices i
join
contracts c
on
c.invoice_id = i.id
group by
c.code
Although the count shows two instead of the desired 1. I understand that this is because of the join on the contract table, but not sure how to fix this.
Try with COUNT(DISTINCT i.id); it should count the different invoice id's in the resultset.
select
c.code, count(distinct i.id)
from
invoices i
join
contracts c
on
c.invoice_id = i.id
group by
c.code
You want the number of invoices?
select count(*)
from invoices i
You want invoices with contracts?
select count( distinct c.invoice_id)
from
contracts
Your code finds number of contracts per each invoice
I am trying to calculate in a SQL statement. I am trying to calculate the total amount in the invoice.total column per customer. I created the following statement:
SELECT customers.firstname, customers.lastname, customers.status, SUM(invoice.total) AS total
FROM customers
INNER JOIN invoice
ON customers.id=invoice.id;
When I run this, I get the total amount in the table. I have 15 different customers in this table but I get only the name of the first customer and the total amount of all customers. What am I doing wrong?
First, you need to Group the data when you want to have aggregate results:
SELECT customers.firstname, customers.lastname, customers.status, SUM(invoice.total) AS total
FROM customers
INNER JOIN invoice
ON customers.id=invoice.id
GROUP BY customers.firstname, customers.lastname, customers.status;
Second, are you sure you are joining the table by correct fields? Is invoice.id correct column? I would expect invoice.id to be primary key for the table and instead I would expect another column for foreign key, invoice.customerid for example. Please double check it is correct.
UPDATE: As it was mentioned in comments, if you have two customers with same first name, last name and status, the data will be grouped incorrectly. In that case you need to add unique field (e.g. customers.id) to SELECT and GROUP BY statements.
You have to add a group by customer (id property for example). If you want to have first and last name in select then you will have to group by them as well.
Use group by clause in your query or you can use common table expression to get the sum of records by customer and then make a inner join with customer table.
You need to add a Group By with column names in your query.
I have a table called sales with two columns vendor_id and customer_id. Both are id's to a row in the people table. I want to grab a sales row by it's id and get the customer name and the vendor name.
How can I do this with MySQL?
Join your people table twice to the sales table (you'll need to alias it at least one time in order to avoid a name collision):
SELECT customer.name AS customer_name, vendor.name AS vendor_name
FROM sales
JOIN people AS customer ON customer.id = sales.customer_id
JOIN people AS vendor ON vendor.id = sales.vendor_id
WHERE sales.id = ?
I have three tables Orders table and customers table and orderstatus table, both order and customer table have customerid as common field,and order and orderstatus have order_status_id as common field
customer table have firstname, lastname, phone and email address fields.
Now if I have to search/select the orders according to customer firstname and/or lastname and/or phone and/or email and/or orderid, then what should be a mysql format of join query so that I can combine all the three tables to get the results from orders table along with orderstatus from orderstatus table?
You can just join the tables. No need for outer joins or anything according to your current data definition.
SELECT *
FROM Orders o, customers c, orderstatus s
WHERE o.customerid = c.customerid
AND o.order_status_id = s.order_status_id
AND c.firstname = 'OM'
AND c.LASTNAME = 'The Eternity'
AND o.orderid = 752
AND ...
Well, I would start with LEFT JOINING all three tables together to start.
Then; structure your where clause to have a line like this for each parameter (basically; only applying filters in the case that parameters are provided)
WHERE
(#customerFirstName IS NULL OR #customerFirstName = customers.FirstName)
AND (#customerLastName IS NULL OR #customerLastName = customers.LastName)
etc...
Depending on the number of records you're expecting; or if performance is lacking; you might want to think about creating a table variable to hold a smaller subset of data. (i.e. create a #customers table; and then only insert into that customers that match customer search criteria. Then join that to your other two tables)
Let me know if i've misunderstood your question; or if you'd like any clarifications.
Something like:
SELECT `orders`.* ,
`orderstatus`.`description`,
`customers`.`firstname`, `customers`.`lastname`, `customers`.`phone`,`customers`.`email`
FROM `orders`
LEFT JOIN `customers` ON `customers`.`id`=`orders`.`customerid`
LEFT JOIN `orderstatus` ON `orderstatus`.`orderid`=`orders`.`id`
WHERE `customers`.`lastname`='blogs'
I'm not sure of your orderstatus table.