update field from another select query - mysql

i am stuck with a simple query if someone can help me on on the same.
i want to update one field with query from another table
the table structure is as follows:-
table stockmain - fields - itemcode, avgcost
table sales - fields - itemid, saleprice, costprice(this field is to be generated with query from stockmain table(avgcost field)
the query is follows:-
$qry = "UPDATE sales SET costprice = SELECT avgcost FROM stockmain WHERE itemcode = 'sales.itemid' ";

You can join these two tables to get one data set, and then copy data from one field to another, e.g. -
UPDATE sales sl
JOIN stockmain stm
ON stm.itemcode = sl.itemid
SET sl.costprice = stm.avgcost;

Related

Combine SQL query for userID with separate query for 2nd table

I'm trying to create an SQL query which filters information by customerID in one table and then combine that with information in another table. I have this query which filters by customerID
$query = mysqli_query($connection, "
SELECT *
FROM booked_activities
WHERE customerID LIKE $_SESSION[customerID]
");
/* INNER JOIN activities ON booked_activities.activityID = activities.activityID);*/
The second part I commented out is where I tried to combine the first half of the query with another table.
The activityID is a common ID in tables: booked_activites and activities.
Right now I can output the first query as activityID but I need to use the activityID to output the activityName from the activities table.
have you tried
$query = mysqli_query($connection, "SELECT *,ac.activityName FROM booked_activities INNER JOIN activities ac ON booked_activities.activityID = activities.activityID
WHERE customerID LIKE $_SESSION[customerID] );
also I would suggest using prepared statement for passing custometr id, instead of using sesstion variable directly in string.
SELECT * FROM booked_activities t1
join activities t2 on t2.activityID = t1.activityID
WHERE t1.customerID LIKE $_SESSION[customerID]

MYSQL Update Table set column equal to select column from same table

I have a new column in my database and I need fill it up with the value of the same column from one specific row. I want to create a feature "copy to all"
For example add the same price to all the products taken from the first row:
ID NAME PRICE
1 PROD1 5
2 PROD2 0
3 PROD3 0
4 PROD4 0
I am trying to select the PRICE of the first row (ID 1) and copy it to all the other rows.
I have tried:
UPDATE PRODUCTS SET PRICE = (select PRICE from PRODUCTS where ID = 1);
I want to end up with this
ID NAME PRICE
1 PROD1 5
2 PROD2 5
3 PROD3 5
4 PROD4 5
But I get this error:
Table 'PRODUCTS' is specified twice,
both as a target for 'UPDATE' and as a separate source for data
I tried specifying each table separately
UPDATE PRODUCTS as a SET a.PRICE = (select b.PRICE from PRODUCTS as b where b.ID = 1);
But I get the same error.
Table 'a' is specified twice,
both as a target for 'UPDATE' and as a separate source for data
Maybe I have to create a temporary table and copy from it?
Any hints on how to accomplish this?
Thanks.
You can do it by nesting the select query:
UPDATE PRODUCTS
SET PRICE = (
select PRICE from (select PRICE from PRODUCTS where ID = 1) t
);
See the demo.
Another way to do it, with a self CROSS JOIN:
UPDATE PRODUCTS p CROSS JOIN (
select PRICE from PRODUCTS where ID = 1
) t
SET p.PRICE = t.PRICE;
See the demo.
This wouldn't work logically, as SQL would try to fetch the data it is updating.
Try running your nested statement select PRICE from PRODUCTS where ID = 1 seperately, saving the response and then running your main statement: "UPDATE PRODUCTS SET PRICE = " + newPrice
If this is a SQL script, i suggest you to break it into 2 queries using a variable:
select #var := PRICE from PRODUCTS where ID = 1;
UPDATE PRODUCTS SET PRICE = #var;
Variables are much more easier than temporary table in my opinion.
I've not fully tested, but the syntax should be that
After looking at many answers on other posts and websites, (none of them had the precise answer), I found the solution with this query, and yes we need temp tables:
CREATE TEMPORARY TABLE tmptable SELECT ID, PRICE FROM PRODUCTS WHERE ID = 1;
UPDATE PRODUCTS SET `PRICE` = (select tmptable.`PRICE` from tmptable where tmptable.ID = 1);
BUT! #forpas solution's is really good and works without creating a temp table.
enjoy.
NEW question: is the temp table removed automatically? Leave me a comment.
Cheers

Update loanbook by reconciling loan stop dates and staff numbers

I have two tables with existing data pulled in from Excel spreadsheets converted to .csv:
table one : loanbook
table two : olb
staffno and loanstart columns are similar in both tables. For every staffno there can be multiple results in olb table.
PROBLEM:
I need to update loanstop column in table one (loanbook) with loanstop values from table two (olb) where staffno and loanstart are the same.
UPDATE loanbook3
SET loanbook3.loanstop = (
SELECT loanstop
FROM olb
WHERE olb.staffno = loanbook3.staffno
AND
olb.loanstart = loanbook3.loanstart
);
RESULT
#1242 - Subquery returns more than 1 row.
What do I do?
use limit 1 with your Subquery
UPDATE loanbook3 SET loanbook3.loanstop =
( SELECT loanstop FROM olb WHERE
olb.staffno = loanbook3.staffno
AND olb.loanstart = loanbook3.loanstart limit 1)

SQL statement - compare two tables columns

I'm new to PHP and SQL.
I want to compare two table-columns.
This is what we have
Table 1(Review)
Table 2(Customer)
We want the Review_Customer_Id to be = to the Customer_Id in the Customer Table
How would you write this?
In my head it would be something like this:
SELECT * FROM Customer
WHERE Customer_Id = Review_Customer_Id IN (SELECT Review_Customer_Id FROM Review);
Check this Answer
SELECT (fistnameField + "" + LastnameField) AS Name FROM Customer WHERE Customer_Id IN (SELECT Customer_Review_Id FROM Review);

Some doubts about this simple INNER JOIN query?

I have the following doubt about this simple INNER JOIN query.
I have these two tables that have to be joined togheter:
The first table is named VulnerabilityFix and contains the following columns:
Id: int identity
FixName: varchar
Vendor: varchar
Title: varchar
Version: varchar
The second table is named VulnerabilityAlertDocument_VulnerabilityFix (this bind the previous table to another table, but this is not important at this time) and contains the following columns:
VulnerabilityAlertDocumentId: int
VulnerabilityFixId: int
Now, on my DB the VulnerabilityFix table contains only an empty record (this record have an id but all the other fields are empty\null), infact if I perform a select *, I obtain:
select * from VulnerabilityFix
Id FixName Vendor Title Version
1
Into the VulnerabilityAlertDocument_VulnerabilityFix I have something like this:
select * from VulnerabilityAlertDocument_VulnerabilityFix
VulnerabilityAlertDocumentId VulnerabilityFixId
78385 1
78386 1
....................................................
....................................................
....................................................
78398 1
Ok, so I want JOIN toghert these 2 table in in such a way that passing the value of the VulnerabilityAlertDocumentId field of the VulnerabilityAlertDocument_VulnerabilityFix table, I obtain all the related record in the VulnerabilityFix table.
So in this case I aspect to retrieve the previous only record that having an id (having a value equal to 1) and all the other fields are empty\null.
So my query is:
SELECT VF.* FROM VulnerabilityAlertDocument_VulnerabilityFix VAD_VF
INNER JOIN VulnerabilityFix VF ON VAD_VF.VulnerabilityAlertDocumentId = VF.Id
WHERE VAD_VF.VulnerabilityAlertDocumentId = 1
The problem is that when I execute this query I obtain an empty set of records and not the unique record that I expetc to obtain.
Why? What am I missing?
Tnx
I think your query should be more like:
SELECT VF.* FROM VulnerabilityAlertDocument_VulnerabilityFix VAD_VF
INNER JOIN VulnerabilityFix VF ON VAD_VF.VulnerabilityFixId = VF.Id
WHERE VAD_VF.VulnerabilityAlertDocumentId = 78385
That is, you are using the wrong column at your ON condition since VulnerabilityFixId seems to be the foreign key over VulnerabilityFix.Id and not VulnerabilityAlertDocumentId.
On the other hand, I can't see any VulnerabilityAlertDocument_VulnerabilityFix.VulnerabilityAlertDocumentId with value 1 in you data set (where condition)