Update Database Table Column From Another Table - php

I have two similar table in my database. I want update my database called qu_time in tbl_quotes from table new_quotes.
I have tried query like this:
UPDATE tbl_quotes
SET qu_time = (SELECT qu_time FROM new_quotes)
but I get an error
1242 - Subquery returns more than 1 row
Let me know if someone have idea to solve it.
Thanks

You should have atleast one common column in both tables.
Use that column in the join condition and do the updating..
UPDATE tbl_quotes t1
JOIN new_quotes t2
ON t1.Id_column = t2.Id_column
SET t1.qu_time= t2.qu_time

Related

How to select and update records in one table by matching ids from it with another table?

I'm using MySQL and phpMyAdmin.
I'm a newbie to the database, RDBMS, SQL queries and all this stuff.
I've one database table called user which has following fields :
user_id(primary key) Data type : int(10)
user_group_id Data type : smallint(4)
I've another database table called user_field which has following fields :
user_id(primary key) Data type : int(10)
country_child_id Data type : mediumint(8)
Now I want select query and update query for above tables.
In select query it should return me the results from table user_field where user_group_id = 6 and user.user_id = user_field.user_id.
In update query I want to update the country_child field(country_child = 1398) from table user_field where user_group_id = 6 and user.user_id = user_field.user_id.
Can some one please help me in building my queries?
Thanks.
Try to get the records you want to update by using INNER JOIN. You can try this query:
UPDATE a
SET a.country_child_id = 1398
FROM user_field AS a
INNER JOIN user AS b ON a.user_id = b.user_id
WHERE b.user_group_id = 6
Hope it helps.
EDIT:
FOR MySQL
UPDATE user_field
INNER JOIN user ON user.user_id = user_field.user_id
SET user_field.country_child_id = 1398
WHERE user.user_group_id = 6
I'm sorry the first update statement will only work in MSSQL. #Kendal is right, in MySQL SET clause comes after the INNER JOIN.
I recommend a where exists clause for this type of update, like this: http://sqlfiddle.com/#!9/0852a/1
update user_field
set country_child_id = 1398
where exists (
select * from user
where user.user_id = user_field.user_id
and user.user_group_id = 6
)
In the sample fiddle, you'll notice that two records are updated -- user_id = 2 and user_id = 6 are both part of user_group_id = 6.
EDIT:
I should mention that I like that syntax because I find it easy to read, though I should probably also mention that you can use a more explicit inner join. Just keep in mind that the syntax for mySQL seems to be slightly different than others, in that the join clause comes before the set clause.
At least, that's how I got it to work: http://sqlfiddle.com/#!9/de73e/1
update user_field
inner join user
on user.user_id = user_field.user_id
set country_child_id = 1398
where user.user_group_id = 6
Try them both and let me know which one's faster. Cheers!

PHP Delete row from table1 if row doesn't exist in table2

I have two tables, one is where the actual posts are, and one for posts that are saved.
I want to delete a row from the saved table if a post is deleted from the website
I tried this:
mysql_query("DELETE FROM `saved` WHERE (SELECT * FROM `ads` WHERE ad_id = `saved`.ad_id) LIMIT 1");
but this doesn't work. Can't think of how to do it the right way
any help it appreciated!
This will delete all rows from saved which its saved.ad_id do not reference to any ads.ad_id
delete s
from saved AS s
left outer join ads as a
on a.ad_id=s.ad_id
where a.ad_id is null;
More here: MySql delete syntax
And here: MySql Multi delete example
you can use left outer join to get the rows exist in table 1 and not exist in table 2 then you can delete it from table 2 using their IDs
mysql_query("DELETE FROM `saved` WHERE `saved`.ad_id not in (SELECT ad_id FROM `ads`)");

How to search for existence of record in one table from another using php and mysql

I use php and mysql. I have two tables,
table A (Id: auto-increment , idno)
table B (Id:auto-increment, sidno).
Table A contains about 3000 records and Table B contains about 27000 records. I want to search whether each of the records in table A exist in table B, if not print the records that does not exist in table B.
I tried to retrieve the records in table A and checking them against table A, but I could not succeed. And it took a very long time to finished the query.
And I have searched throughout but could not get something like this.
Please can anybody help me.
Thanks!
The following query might return all the idno which are not in table B
SELECT * FROM tableA WHERE `idno` NOT IN (SELECT `sidno` FROM tableB)
SQL Fiddle Demo
Ok. Try this:
SELECT tableB.Id, tableB.sidno
FROM tableA
RIGHT JOIN tableB ON tableA.Id = tableB.ID
WHERE tableA.Id = 'NULL';
This should give you all the records you want.
like this
select * from tableA where Minus select id from where tableA.id=tableB.id;
MINUS
http://www.techonthenet.com/sql/minus.php
Try this
SELECT * FROM table2 t where sid NOT IN (select id from table1) ;
Demo

Msql Inner Join Query Dilemma - Need Joined Auto Increment Value

I have two mysql tables that I am querying together and the query works fine. The tables are car and requests
The problem is that i need both the auto increment id's and it only gives me one.
The one it gives me is not the joined tabled.
Here is my query so far
SELECT * FROM `car` INNER JOIN `requests` ON `car`.`make_id` = `requests`.`make_id` WHERE `car`.`user_id` =21
I just need to someway get the auto increment id of the requests table.
As always stack exchange is the best place to come for answers so thanks in advance!
Just specify your query as this and you'll get both but with different names:
SELECT `car`.id AS car_id, `requests`.id AS request_id, *
FROM `car`
INNER JOIN `requests` ON `car`.`make_id` = `requests`.`make_id`
WHERE `car`.`user_id` =21
Note that you will still have an ID column which SHOULD be the requests.id, just diregard it and use car_id and request_id...
When you have to join tables and table contains same name than it creates an ambiguous situation. So always use table name or table alias with column name Like this
SELECT t.column
FROM table as t
OR
SELECT table.column
FROM table

Creating a table from a query in MySQL/PHP

I'm not sure this is possible, but the code I tried using first is:
mysql_query ("CREATE TABLE My_table as ( SELECT * FROM CPE LEFT JOIN Greenwich_j20 ON CPE.cust_index = Greenwich_j20.cust_index LEFT JOIN Liverpool_j20 ON CPE.cust_index = Liverpool_j20.cust_index)")
or die ("this certainly didn't work\n");
The query itself works fine, and the syntax for the table works fine, but the combination is what it really doesn't like. Does it have a problem creating a table from a left join query?
If there is any auto increment column in your existing tables that will be not remain auto_increment in the table to be created.
You could try by selecting columns instead of * there should be an error of column Ambiguous.

Categories