简体   繁体   English

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

[英]Querying the data from sql table using non key column

let's say I have a table in my DB called songs .假设我的数据库中有一张表,名为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谢谢

Assuming your first query will be some sort of search... You might also want to read up on the like operator.假设您的第一个查询将是某种搜索...您可能还想阅读 like 运算符。

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.假设在song_title 上没有索引,查询将[不会使用索引进行搜索。 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希望有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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