If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Replace value in MySQL table by condition from two tables

Started by ruschan, Oct 05, 2022, 12:57 AM

Previous topic - Next topic

ruschanTopic starter

There are 2 tables available: Parents (ID card, name, age) and Children (ID, name, age, parent_id), where parent_id represents the ID of the corresponding parent.

To update the age of a child to 10, a query can be used that combines both tables. More specifically, we join the Parents and Children tables using their IDs and specify the condition to check if the child is more than 10 years old and their parent is less than 40 years old. The resulting query will update the age of the child to 10 only if the aforementioned conditions are met:

SELECT p.name, p.age, c.name, c.age
FROM parents p JOIN children c ON p.id = c.parent_id
WHERE p.age < 40 AND c.age > 10

Regarding the IbExpert script, it aims to modify a product's price in a particular store by decreasing it by 10%. However, a mistake occurs during its execution. The script uses a JOIN statement to connect the Products and Stores tables using their IDs and then changes the price of the corresponding product. Nonetheless, the error indicates that one of the tables cannot be found or accessed. Further examination of the script and the database schema may help solve the issue.
  •  

KiranaTama

I'm not entirely confident about whether Firebird can handle this query, but let's give it a try. I haven't worked with it for some time. If the query fails, we can always switch to the alternative solution.

The mentioned query targets the Incomes32 table and sets the value of the facialacc_cls attribute to 15,000,000 if it has the default, i.e., zero value and meets a specific condition. The condition involves checking if a corresponding record exists in the Quotestitle table with an acceptdate different from 20220000 and the same ID as the current record in Incomes32. To carry out this check, we use a subquery within the EXISTS clause.

Here is the original query for reference:

UPDATE incomes32
  SET facialacc_cls = 15000000
  WHERE facialacc_cls = 0 AND
        EXISTS(SELECT *
                FROM quotestitle
                WHERE quotestitle.id = incomes32.recordindex AND
                      quotestitle.acceptdate <> 20220000)

I hope this helps!
  •  

John008

The original query attempted to utilize a JOIN operation on the Parents and Children tables using their IDs and included conditions involving the age of the parent and the child. However, the where clause seemed to have conflicting conditions that did not align with the requirement of updating the child's age to 10.

A revised SQL query for achieving the desired update would be as follows:

UPDATE Children
SET age = 10
WHERE age > 10;


In this refined query, the UPDATE statement is straightforward and does not involve joining with the Parents table or considering the age of the parent. It simply updates the age of any child that is currently older than 10 to 10, fulfilling the specified requirement.

Moving on to the IbExpert script, the error message indicating that one of the tables cannot be found or accessed necessitates a methodical approach to troubleshoot the issue. First, it's essential to verify the existence of the Products and Stores tables in the database schema that the script is connecting to. This can be achieved by examining the database using SQL commands or a database management tool.

Next, the script itself should be methodically reviewed to ensure that the table names are correctly specified and that the database schema being accessed matches the expected schema. Any discrepancies in the table names or the schema utilized in the script could result in the error message indicated.

Furthermore, it's crucial to confirm that the database connection parameters in the script, such as the database URL, username, and password, are accurate and enable access to the intended database.

After addressing these potential issues, the script can be re-executed to ensure that the intended update operation – decreasing the price of a specific product in a particular store by 10% – is successful. This would involve using a valid SQL UPDATE statement on the Products table, potentially involving a JOIN with the Stores table based on their IDs, and applying the necessary logic to reduce the price by 10%. Additionally, it may be beneficial to include error handling in the script to capture and handle any potential issues that could arise during its execution.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...