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
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
I have a web application in which I want to create a section to fill receipt information.
I need some information such as customer ID, customer name, customer address, customer tellephone number, product ID, sales agent ID, order number, and order date. The customer ID, order number, order date, agent ID and product date will be stored in one table.
Will it be better to
generate the customer id using the mt_rand() function in PHP based on some criteria so that this info gets directly to the order table without any further queries or to
generate this ID in MySQL and query the database again to get this info and store in the order table (and I will need to store the customer ID in who bought a specific product in the orders table).
Which approach is better? Is there another approach that I should consider?
You can make use of AUTO_INCREMENT in MySQL. For example,
create table
( INT AUTO_INCREMENT) )
Let the id generation be taken care of MySQL instead of having your code generate a number and store it in database
you can also use to SELECT uuid() in phpmyadmin
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.
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 ");
I have a software in PHP and postgres that I use for invoicing. I am running a stock management system that I created. i am trying to create a stock movement page where I can see when a part came in, where it was issued and when and also when it was credited (if it was). I am running 5 tables for the stock. My main one is part2vendor, parts, expenses, wo_parts and int_part_issue. When I receive stock, it goes into the table part2vendor (onhand gets increased by number received). The expense table gets the details of the part number, the supplier invoice and the date received. wo_parts stores the part number issued to a workorder. int_part_issue is when I do a stock adjustment or use a part internally. I am looking to create a PHP table that would list (in date order) the 'paper trail' of a part. I can let you know table names and columns if required. Thanks.
Sounds like you need a simple history table?
Columns
part_history
id
part_id
date_modified (timestamp)
action ( or maybe action_id if you have an actions table)
vendor_id
And when you get a new part, and add it to the parts2vendor table ( i think u said) you would use the inserted part ID (or unique part id ) to add a record rto history
INSERT
(id, part_id, action, vendor_id)
46565, 5757575, "Purchased", 757575
The date will be inserted as a timestamp by postgres
Than for any part yuou can grab history relying on the uniquer id
sort by date_modified DESC.