I have two tables namely invoice and purchase_list.
INVOICE :
invoice_number,
invoice_date,
customer_id,
invoice_purchase_value,
invoice_tax,
invoice_bill_amount
PURCHASE_LIST
purchase_id,
invoice_number,
customer_id,
purchase_rate,
purchase_quantity,
purchase_value
For a particular bill, i insert the bill's main details in invoice table and its corresponding products purchase list in purchase_list table.
Usually many products been purchased in one invoice.
Here comes the problem. One insert statement is enough for invoice table.
But, for purchase_list, multiple insert statements required.
(For ex, If 6 items purchased in one invoice, 6 insert statements should be executed.)
I am in thought of sending all purchased values in array format from PHP. If so, how can i extract values from those arrays and insert it into purchase_list table or any other ways to implement the same..?
I am newbie to MySql. Please brief in detail.
Thanks in advance.
look into PDO prepared statements.
Related
I'm really struggling on this. I'm migrating from one shopping cart CMS to another. I've got to move the past orders data. The database's are structured differently so I need to script the migration. The SQL query I'm coming up with is:
SELECT tblCustomers.*, tblAddress_Book.*,
(SELECT GROUP_CONCAT(orders_id) FROM orders WHERE customers_id = tblCustomers.customers_id)
FROM customers as tblCustomers
JOIN address_book as tblAddress_Book
ON tblCustomers.customers_id = tblAddress_Book.customers_id
Ideally what I'm looking to do is to pull the data in a format as such
all columns from customer's table, all columns from address book in relation to customer's id, the orders made in relation to customer's id if exists, I need to join the data from orders table into one cell per customer id
I still want to grab the customers that didn't make orders as well.
I hope I explained that clearly enough. I've been searching and tinkering with it for hours but I'm not getting any closer. Hopefully someone can help
How would I go about setting up several tables that need to pull info from one another?
Here's my scenario...
Table Products (15 different items)
id, name, price, features
Table Orders
id, user_id, date, time, products, total_price
My problem is that when an order comes in it can have a variety of combinations when it comes to the products selected. Ex: 4x items with (id=3), 7x items with (id=9) etc etc...
How do I store that so that it can be easily retrieved and fast to process without over complicating things?
One way I can think of is storing products as a json array. Something like:
{"4":3,"7":9,"1":14}
I've never worked with stuff like that before so no clue how challenging data retrieval would be.
Any tips are greatly appreciated...
You need one to many, where a table order could have many products, you need to have a pivot table where you put the id order, id product and the number of products. When you do want to find all the products in an specific id order you will know all the products searching by the id order.
Here's an example:
DataBase design: one to many database?
I am having trouble in writing a SQL query for php query, (for the Premiere Product Database). I have to get the total number of orders placed by a customer for a given customer number.
SELECT customername, firstname, lastname, COUNT(DISTINCT(customernum))
FROM customer, orders, rep
WHERE customer.customernum=orders.customernum
ANDcustomer.repnum=rep.repnum AND customer.customernum=customernum;
The code above gives me an error message "#1052 - Column 'customernum' in field list is ambiguous".
This means two or more of the tables concerned in your query have a field called customernum and it doesn't know which you're referring to. Prefix it with the table name.
COUNT(DISTINCT(your_table_name.customernum))
It worked when I removed the DISTINCT and customer.customernum=customernum; parts. Thank you
I have a table called jobcardlabour. It has 3 colums, Jobcard, Description, Quantity and Amount. When I create a jobcard I enter the work I have done into this table. I know want to create an invoice so I want to call this table and display the results in a html table on the printable invoice. I can do this but where I am having difficulty is there might be 3 or more rows which all have the same jobcard number. I need all the rows displayed in the table. The table needs three headings, called Description, Qty and Amount. If anyone can help with the basics I can modify it to suit my needs. Many thanks again.
If you need all the information, then just do
SELECT * FROM jobcardlabour;
I need to keep all purchases from a single client together on a database table (It will be atored in my Orders table) and once someone is browsing a product, the system will search the dB for clients that bought that product and recommend the other products they bought (you will need to check for popularity of the other products and avoid repetition)
The data will be stored in a mysql database in a table called Orders. I then need to be able to search that database to see if other people have bought this product and if so which products they also bought.
So I've come up with this query
SELECT ProductName FROM Orders
(I have little knowledge and would like to no if I am on the right track)
I need to keep all purchases from a single client together on a database table
For this one you probably need to create a horizontal view (restricting by the client id you need to monitor the purchases) CREATE VIEW.
Could you provide your database schema ? in order to create the query you need you should join many tables, so it would be easier to provide the schema and how your tables are tied together.
try this
SELECT ProductName
FROM Orders WHERE client_id in
(SELECT client_id
FROM Orders
WHERE productname="ProductName ");