Reputation: 866
I have 2 tables table1 and table2:
id
with a value 1212
(example) in it.action
with a data like added 1212
or updated 1212
.Now check this query.
SELECT t1.*, t2.name
FROM table1 t1, table2 t2
WHERE t2.action LIKE 'added t1.id'
I want to add the name
field in the second table, to first one with the added
(updated
not needed) data only.
What is the where clause for this condition?
How can I query this?
Upvotes: 1
Views: 1310
Reputation: 866
I did it like this.
SELECT t1.*, t2.name
FROM table1 t1, table2 t2
WHERE t1.id = (RIGHT(t2.action, 4)) AND t2.action LIKE 'added %'
thanks to all who tried to help me.
Upvotes: 2
Reputation: 160833
It is better to divide action
column to two columns, one for action type, and one for action target. Then you could join the two tables.
SELECT t1.*, t2.name
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.action_target
WHERE t2.action_type = 'added'
Upvotes: 1