TharinduLucky TharinduLucky - 2 years ago 133
PHP Question

Querying the data from sql table using non key column

let's say I have a table in my DB called

. The columns are
id, song_title, author

So, instead of querying like this,

SELECT * FROM songs WHERE id = 23

Is it OK (no problem at all) to query like this..?

SELECT * FROM songs WHERE song_title = 'Rain Over Me'

Will there be any side effects ?


Answer Source

Assuming your first query will be some sort of search... You might also want to read up on the like operator.

The way you have your query written at the moment, the text will have to match the title exactly before it will be returned. If you intended this to be a search, your users will find something like this easier to use.

SELECT * FROM songs WHERE song_title LIKE '%Rain%'

This will return all songs where 'Rain' is in the title.

As for the side effects...

Assuming there's no index on song_title, the query won[t be using an index to search on. That said, you can create more than one index per table, and an index doesn't have to be on the primary key of the table. An index can also consist of multiple columns.

I'm guessing you'll be fine without an index anyway ;-)

Hope that helps

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download