简体   繁体   中英

SQL SERVER Query Formation on matchin field data

I have two tables. I need a certain output.

Table1
======================
| PropertyID | Name  |
+------------+-------+
| 1          | Pants |
+------------+-------+
| 2          | Dress |
+------------+-------+

Table2
===============
| Description |
+-------------+
| 2-Pants     |
+-------------+
| 1-Dress     |
+-------------+
| Dress       |
+-------------+

Output
==============
| PropertyID |
+------------+
| 1          |
+------------+
| 2          |
+------------+
| 2          |
+------------+

Now I need the ProperyID where Name matches the Description field of Table2 . It does not have to be an exact match. Like "Pant" should match with "2-Pant". I am not able to form a proper query for it. Help please?

You need to RIGHT JOIN Table1 to Table2 and use the LIKE condition appending the % wildcard on two sides of the value from Table1. Use the following query:

SELECT T1.PropertyId FROM Table1 AS T1
RIGHT JOIN Table2 AS T2 ON T2.Description LIKE '%'+T1.Name+'%';

SQLFiddle demo here.

Extra SQLFiddle demo, so you know that the right column is being matched.

Try this

SqlFiddle

select t1.PropertyID from  Table2 t2 left join Table1 t1 on t2.Description 
like '%'+t1.name+'%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM