Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Like with a subquery

Tags:

How can i make this work?

SELECT *  FROM   item  WHERE  item_name LIKE '%'                        || (SELECT equipment_type                            FROM   equipment_type                            GROUP  BY equipment_type)                        || '%'  

The inner sub query returns a list of strings like 'The' 'test' 'another' and i want to select all items from the item table where the item_name is similar to the sub queries return values. I need to have the wild cards.

Is there an alternative where i can use wildcards but use the IN sql command instead?

like image 421
jordan Avatar asked Jun 18 '13 19:06

jordan


People also ask

Can we use subquery in like Operator?

Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements along with expression operator. It could be equality operator or comparison operator such as =, >, =, <= and Like operator. A subquery is a query within another query. The outer query is called as main query and inner query is called as subquery.

Why CTE is better than subquery?

CTE can be more readable: Another advantage of CTE is CTE are more readable than Subqueries. Since CTE can be reusable, you can write less code using CTE than using subquery. Also, people tend to follow the logic and ideas easier in sequence than in a nested fashion.

Can a subquery be multi valued?

A multi-value subquery retrieves more than one value and has two forms of syntax, as shown below.

Can we use subquery in between clause?

A subquery cannot be immediately enclosed in a set function. The BETWEEN operator cannot be used with a subquery. However, the BETWEEN operator can be used within the subquery.


1 Answers

You can use an INNER JOIN:

SELECT I.*  FROM item I INNER JOIN (SELECT equipment_type              FROM equipment_type              GROUP BY equipment_type) E     ON I.item_name LIKE '%' || E.equipment_type || '%' 
like image 73
Lamak Avatar answered Nov 02 '22 08:11

Lamak