Copy rows in MySQL table multiple times and adding additional column - php

I have created MySQL table in database. Table name is products and the columns are( prodict_id(pk) product_name and pack_size) as shown in the figure below.
What I want to do is , copy all the rows in the table and add additional information in additional column called (buyer_name) so each product is associated with a specific buyer which makes it unique
Is there a way I can achieve this using query? Where I can give a list of buyers and it attaches it to all rows in table?
p.s I have almost 700 rows in my table and I have 12 buyers, so if I do it manually, it will consume too much time

As per your comment your buyer details are in a table and you want to map each product with each of the buyer then you can write your insert query like below:
insert into newtable
select t1.*, t2.buyername from products t1 join buyers t2
DEMO
You can use where clause also to filter some results from either of the table.

It seems you want to automate the data insertion from product table to buyer table. How about, if you select that fetches all the buyers first and then you insert into buyer table.
It can be based on subquery where insert is the outer one and select is the nested one.
Good luck !

Related

Query selected columns from two tables with a Condition clause

I have two tables-
1) ****Company_Form****
[Contract_No#,Software_Name,Company_Name,Vendor_Code]
2) ****User_Form****
[Contract_No,Invoice_No#,Invoice_Date,Invoice_Amount,Invoice_Submit_Date]
Fields denoted with # and bold are primary keys.
=>The user has to enter a software name for which he wants to get the data of.
=>I have to structure a query in which I have to display the result in the following form:
[Contract#,Software_Name,Company_Name,Invoice_No,Invoice_Date,Invoice_Submission_Date]
Now,
one Contract_No can contain many Invoice_no under its name in
the User Form table.
One Contract_No can occur one time only in
Company_Form table
The retrieved records have to be group by the latest Invoice_Date
I came to the logic that:
I have to first retrieve all the contract numbers with that software
name from Company_Form table.
I have to query that contract number from User_Form table and display
the data for each matched contract no. fetched from Company_Form
table.
The problem is that I am unable to structure a query in SQL that can do the task for me.
Kindly help me in formulating the query.
[PS] I am using SQL with PHP.
I tried a query like:
I tried one approach as :
SELECT a.ContractNo,a.SoftwareName,a.CompanyName,b.InvoiceNo,b.InvoiceDate,b.InvAmount,b.InvoiceSubmitDate
FROM Company_Form as a,User_Form as b
WHERE b.ContractNo IN(SELECT ContractNo FROM Company_Form WHERE
SoftwareName='$Sname') AND a.ContractNo=b.ContractNo;
But I am getting a error that sub query returns more than 1 row.
Can I get help from this?
I am assuming you are attempting to find the most recent price of the user selected software and its corresponding invoice. Here is an approach to do this. If this is tested to your satisfaction, I can add necessary explanation.
select uf.Contract_No#,
cf.Software_Name,
cf.Company_Name,
uf.Invoice_No#,
uf.Invoice_Date,
uf.Invoice_Amount,
uf.Invoice_Submit_Date
from User_Form uf
inner join (
-- Most recent sale of software
select Contract_No#, max(Invoice_Date)
from User_Form
group by Contract_No#
) latest
on (
-- Filter via join for latest match records
uf.Contract_No# = latest.Contract_No#
and uf.Invoice_Date = latest.Invoice_Date
)
inner join Company_Form cf
on cf.Contract_No# = uf.Contract_No#
where cf.Software_name = :software_name
If the requirement allows your sub query to return more than one row, I would suggest you to use IN instead of = in the where clause of your main query.
Please note that I have just looked at the query and have not fully understood the requirements.
Thanks.
I worked around for some time and finally came to the following query which works like a charm
SELECT a.ContractNo,a.SoftwareName,a.CompanyName,b.InvoiceNo,b.InvoiceDate,b.InvAmount,b.ISD
FROM Company_Form as a,User_Form as b
WHERE b.ContractNo IN (SELECT ContractNo FROM Company_Form WHERE SoftwareName='$Sname')
AND a.ContractNo=b.ContractNo;
If anybody needs help in understanding the logic of this query,feel free to comment below.

Unique Columns in SQL Views

In my database I have one table that contains a complete list of products, and another table that contains the same list of products on the x-axis, with a list of customers on the y-axis, where the value for each product can be 1 or 0 depending on whether that customer can view that product. My SQL looks like this:
SELECT products.product_code, products.product_type, products.product_category, products.product_title, products.product_description
FROM product_lists
INNER JOIN products
ON product_lists.product_code=products.product_code
WHERE product_lists.customer="1"
ORDER BY products.product_code
My problem is that I would like to create a view of this result for each customer to use as that customers product table, however when I create it I get the message "This table does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available." even though the product_code field is set as a primary key in both the products table and the product_lists table.
How can I create a join/view that uses the primary key from the table(s) it was created from? In short I would like the product_code field to become the primary key of my view.
Thanks!
I think the problem is the join. You can fix this by moving the condition to the where clause. MySQL doesn't allow subqueries in the from, but it does in the where:
SELECT p.product_code, p.product_type, p.product_category, p.product_title, p.product_description
FROM products p
WHERE EXISTS (SELECT 1
FROM product_lists pl
WHERE pl.product_code = p.product_code AND
pl.customer = 1
)
ORDER BY p.product_code;

How to add multiple registries in a table mysql and php

I have two tables:
Users
uid [pk]
Transactions
uid[fk]
amount
With a one to many relation between both of the tables, so now I want to add all the transactions for every user and show them in a table like this:
UID -> balance
Idon't really know how can I make do this for every user in my table...
Any ideas?
I don't want to store the results, I just want to show them on a table, and after that I'll export that table to Excel.
This SQL query will do the trick:
SELECT uid, SUM(amount) FROM Transactions GROUP BY uid

How to join multiple fields data from two different tables in SQL?

I'm using this query to join data from two different tables. Invoice table holding data, with stock code and supplier code. Please check my query point out my error in query.
inv_code field holding stock code and supplier code. My second table is
cb_chart_temp having acc_code, and acc_name;
SELECT
`invoice`.`inv_code`,
`cb_chart_temp.acc_name`,
`invoice.sup_id`,
`cb_chart_temp`.`acc_name`
FROM
`invoice`,
`cb_chart_temp`
WHERE
inv_no LIKE 'PI%'
invoice.inv_code=cb_chart_temp.acc_code
AND invoice.sup_id=cb_chart_temp.acc_code
My result should look like this!
inv_code acc_name sup_id sup_name
ST-00001 Stock Name SUP-00001 Supplier Name
It misses an AND, as already pointed out by #Lion, plus it is doubtful that the join of keys is correct. As you have it, inv_code, acc_code, and sup_id all belong to the same domain, i.e., all of them are, e.g., invoice codes.

php/mysql add rows together to get total

here's the scenario. I am generating a report of all the members who have dues to pay for a certain time period.
I am successfully selecting and displaying each database entry as a row in a html table.
The problem is the total fields the report must have. Each member pays different amounts based on what services they use, so I must add the values in each field individually to ensure proper result.
Question is, how do I go about adding the rows/field together?
Edit:
To clarify. I am adding dues paid and donations paid fields. They are classified and integer in mysql database.
Example, let's say that my query returns 3 results. I wish to add the dues paid for all 3 results and display it as total_dues_paid. Same idea for donations.
This must be dynamic for any given number of results, as this changes month to month and we have seen several hundred results in some months.
Thanks
To add fields (columns):
SELECT col1, col2, col3, (col1+col2+col3) AS Total FROM table;
To add rows together, use the SUM() aggregate:
SELECT
userid,
SUM(col1) AS col1_total,
SUM(col2) AS col2_total
FROM table
GROUP BY userid
You can add in your query string.
SELECT (field1 + field2) AS Total
FROM table

Categories