使用非键列查询sql表中的数据

时间:2022-01-28 09:08:37

let's say I have a table in my DB called songs. The columns are id, song_title, author.

假设我的数据库中有一个叫做歌曲的表。列是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 ?

会有副作用吗?

Thanks

1 个解决方案

#1


1  

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.

这将返回标题中“Rain”所在的所有歌曲。

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.

假设song_title上没有索引,则查询不会使用索引进行搜索。也就是说,您可以为每个表创建多个索引,并且索引不必位于表的主键上。索引也可以包含多个列。

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

无论如何,我猜你没有索引会好的;-)

Hope that helps

希望有所帮助

#1


1  

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.

这将返回标题中“Rain”所在的所有歌曲。

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.

假设song_title上没有索引,则查询不会使用索引进行搜索。也就是说,您可以为每个表创建多个索引,并且索引不必位于表的主键上。索引也可以包含多个列。

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

无论如何,我猜你没有索引会好的;-)

Hope that helps

希望有所帮助