-1

I have a table on MySQL, called Items with 2 columns: Id and Name. I want to show my items, filtering by its name first character. To do this, now I'm using this query:

Select Id,Name from Items WHERE Name LIKE 'a%'

I have 2 questions:

1) It is this the best method to achieve that?

2) To create the filter view, I want to know which characters have at least one item name starting with it. For example, I don't have any item name starting with "X". How could I know which ones have with a single query?

Thanks

4

3 回答 3

1
  1. 如果您有一个索引,Name那么您的查询就可以了。

  2. 获取所有具有名称的字符

    Select substr(Name, 1, 1) as starting_character
    from Items 
    group by starting_character
    order by starting_character
    
于 2013-07-28T00:39:18.953 回答
0

您可以运行大型 OR 查询,例如......

 select * from Items where nobre like "A%" or nombre like "B%" or nombre like "C%" //etc etc

但是如果速度和表格大小是一个问题,我会创建另一列,然后将字母值作为 INT 输入到该列中。有点像索引。

因此,当您插入任何新值时,只需编写一个函数以返回第一个字母的字母值,并将其插入新列。

现在,您可以根据该整数查询列中的任何字母。

希望清楚地出来,我有时不太擅长解释事情......

于 2013-07-28T00:38:22.980 回答
0

这将为您提供所有第一个字符 - 仅一次(因此 DISTINCT)

SELECT DISTINCT SUBSTR(Name,1,1) FROM Items ORDER BY SUBSTR(Name,1,1)

然后您可以使用它来创建您的过滤器。

于 2013-07-28T00:41:16.583 回答