i have two tables named t1 and t2.
t1 have 500 rows and t2 have 220000 rows. the relationship between these tables are t1.t_id and t2.t1_t_id. not all t1 rows have corresponding row in t2.
now this line of code works fine
SELECT t1.t_id , t2.t1_t_id FROM t1, t2
where t2.t1_t_id = t1.t_id and t1.t_id=11
but this does not work
SELECT t1.t_id , t2.t1_t_id FROM t1, t2
where t2.t1_t_id = t1.t_id
in case picture not available
error is:
1 error were found during analysis
1. missing expression (near "ON" at position 25)
SQL query edit
set foreign_key_checks=on
mysql said
2006 server has gone away
what i want to do after solving this issue is:
t1 have a column named p_id which is primary.
i recently added a new column in t2 named t1_p_id.
now i want to update t2.t1_p_id = t1.p_id where t2.t1_t_id = t1.t_id.
i know i should use inner join on t2.t1_t_id = t1.t_id in a update query but it also issues the same error.
my guess for the error is that i should specify some specific condition because not all t1 have corresponding row in t2
update
i am querying by browser by mysql console
thanks
Related
I'm trying to "merge" two tables and have found a few examples but I'm having difficulty applying them as it continues to say I have syntax error:
UPDATE T2
SET payable_id = T1.payable_id, payable_type = T1.payable_type
FROM payments_distributions AS T2
JOIN payables AS T1
ON T1.payments_distribution_id = T2.id
It mentions that the FROM is at an invalid position at the moment.
I'd appreciate the help. Thanks
Move the SET clause to the end and all of the table references after UPDATE.
UPDATE payments_distributions t2
INNER JOIN payables t1
ON t1.payments_distribution_id = t2.id
SET t2.payable_id = t1.payable_id,
t2.payable_type = t1.payable_type;
I have been going through many queries, but can't seem to find the right combination to get this to work. I get the error "You can't specify target table 't1' for update in FROM clause". I know MySQL doesn't like the subquery in the update statement and have read about wrapping it in other select statements, but can't seem to figure it out. Here is a stripped down query of what I am looking for:
UPDATE myTable t1 SET t1.num=concat(t1.num,'B') WHERE t1.num in ('1','2') and t1.expiry=(SELECT max(t2.expiry) from myTable t2 where t2.num=t1.num);
Basically trying to get the latest date (expiry) for each number (num) and change the number where applicable.
This should works :
UPDATE myTable t1, (SELECT num, max(expiry) expiry from myTable t2 group by num) t2
SET t1.num = concat(t1.num,'B')
WHERE t1.num in ('1','2')
and t1.expiry = t2.expiry
and t1.num = t2.num;
I am trying to set a column value in MySQL based on the following query.
select * from table1 t1
join table2 t2
on t1.id = u.t1_id
and t2.status = 'verified'
and not exists (
select 1
from table2 t2_2
where t2.t1_id = t2_2.t1_id
and t2_2.updated_at > t2.updated_at
)
This query returns the results I want, but when I try to add
SET t1.column_k = 'some value'
to the end, I'm getting an error that simply says You've got a syntax error near set t1.column_k.... check manual corresponding to your version of MySQL.
I'd really like to know how to include a set on the results of this query and am having trouble formulating that. Any help or ideas?
It's difficult and confusing to me I think because of the self join. The eventual plan is to port this query w/the set command into a migration file in rails once I've got it working.
You need an update. Select isn't used for setting values.
update table1 t1 join
table2 t2
on t1.id = u.t1_id and
t2.status = 'verified' and
not exists (select 1
from table2 t2_2
where t2.t1_id = t2_2.t1_id and
t2_2.updated_at > t2.updated_at
)
set t1.column_k = 'some value';
I like to join two tables T1 and T2.
T1 is my left table and T2 is my right.
`SELECT
DISTINCT T1.name AS 'name',
T1.volume AS 'volume',
T1.vserver AS 'vserver',
T1.cluster AS 'cluster',
T1.snapmirror_label AS 'snapmirror_label',
T1.timestamp AS 'timestamp'
FROM
schema3.snapmirror_policy_rule snapmirror_policy_rule,
schema3.vserver vserver,
schema3.cluster cluster,
schema1.T1 T1
LEFT JOIN
schema2.T2
ON schema1.T1.name = schema2.T2.name
....
`
My question is, if the table T2 doesn't exist, how will I perform?
My idea is to join two tables if both the tables (T1 as well as T2) exist, else based on few conditions, I will select rows from my T1 table (which exists always) only. I'm looking for the query in these contexts.
PS:- In my working env, procedure like complex thing will not work. Looking for simple straight forward MySql query.
I'm using MySQL 5.1.41 on ubuntu 12.10 and MySQL Workbench.
I have 2 product tables, t1 and t2. t1 is the live data and t2 is a imported data ready to be updated into t1 to update all the new product prices. So I run:
SELECT * FROM t1
JOIN t2 ON t1.id = t2.id
WHERE t1.price != t2.price;
This returns 1201 records where the price is different and needs to be updated. So I run:
UPDATE t1 JOIN t2 ON t1.id = t2.id
SET t1.price = t2.price
WHERE t1.price != t2.price;
This completes without error and reports 1143 row(s) affected, Rows matched: 1143 Changed: 1143 Warnings: 0
So already something here is not right. 1201 records were different in the select query, but only 1143 changed using the same join and criteria?
Running the initial select query I'd expect to see 58 records that still had different prices. But when running it I get the same 1201 as I did initially. It's as if the updates are not being committed.
Any ideas?
The number (1201) that your SELECT shows is not records of t1 but rows from the JOIN of two tables. If the two id are not both UNIQUE or PRIMARY KEYs then this is expected. Some rows of t1 match multiple rows from t2. But when the UPDATE is done they are only updated once (this is a MySQL "feature" or "bug" of UPDATE that checks WHERE conditions sequentially during an update statement.
Try this to see how many rows (of t1) should be updated:
SELECT * FROM t1
WHERE EXISTS
( SELECT *
FROM t2
WHERE t1.id = t2.id
AND t1.price != t2.price
);