Krishna Raj
Krishna Raj

Reputation: 866

How to match part of a field with data in another field with query?

I have 2 tables table1 and table2:

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

Answers (2)

Krishna Raj
Krishna Raj

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

xdazz
xdazz

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

Related Questions