Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Keeper of the Cog Rebecca
Keeper of the Cog Rebecca
480 Points

I just finished this lesson, & it might be covered on later, but I would like to be able to join multiple LIKE commands

In the same way you can use the IN command to compare one variable with multiple values, I would like to be able to use LIKE command to be able to compare LIKE against multiple values.... is this possible?. For example, see below code... I would like to combine the LIKE commands without having to retype the variable and other values all over again.

I tried several things that did not work lol.

SELECT title, first_published FROM books WHERE first_published LIKE "2%" OR first_published LIKE "18%";


I was trying to combine it like this, and I tried a lot of things, but obviously it did not work. Does anyone know a shortcut?

SELECT title, first_published FROM books WHERE first_published LIKE IN ("2%", "18%");

******End of Line

Keeper of the Cog Rebecca
Keeper of the Cog Rebecca
480 Points

This is for SQL btw, I forgot to mention that!

1 Answer

Steven Parker
Steven Parker
231,261 Points

As you discovered, you cannot combine LIKE with IN. When using LIKE you combine tests with OR just as you showed.

If you get really advanced, you can go beyond SQL and use programmable features of the database engine and create your own functions, procedures and/or packages to do things like this.