Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update with Join in SQLite

Tags:

sqlite

I have 2 tables and like to update one of them with the values from the other.

software --------- id , purchprice  softwarecost ------------ id , purchprice 

I've tried these queries but, SQLite doesn't support JOINS with UPDATE.anybody out there who can come up with a query for this.thanks for your help.

UPDATE software  SET software.purchprice=softwarecost.purchprice  WHERE software.id=softwarecost.id  UPDATE software  INNER JOIN softwarecost on software.id=softwarecost.id  SET software.purchprice=softwarecost.purchprice  
like image 456
Carter Avatar asked Oct 09 '13 11:10

Carter


People also ask

Can we use update with joins?

The most easiest and common way is to use join clause in the update statement and use multiple tables in the update statement. Here we can see that using join clause in update statement. We have merged two tables by the use of join clause.

How do I update from a select in SQLite?

First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause. The WHERE clause is optional.

Can you do joins in SQLite?

SQLite Joins clause is used to combine records from two or more tables in a database. A JOIN is a means for combining fields from two tables by using values common to each.

What does SQLite update return?

Returns the current database page size, in bytes.


1 Answers

This will work

UPDATE        software SET purchprice = (SELECT purchprice                   FROM softwarecost                   WHERE id = software.id)  where EXISTS (SELECT purchprice                   FROM softwarecost                   WHERE id = software.id) 

Here we use exists because without that the query will set software.purchprice to null if no "correlated" row is found.

like image 124
Durai Amuthan.H Avatar answered Nov 12 '22 05:11

Durai Amuthan.H