尝试获取数据并像字典一样打印。 表格: blog_tags
id name
1 atag1
2 atag2
3 dtag1
4 etag1
5 etag2
6 ctag1
7 ctag2
8 ctag3
9 ztag1
我希望数据输出为:
A
atag1
atag2
C
ctag1
ctag2
D
dtag1
E
etag1
etag2
Z
ztag1
从此开始:
select name from blog_tags order by name;
mysql查询的内容是什么?
答案 0 :(得分:2)
尝试这样的事情
select name
from (
select distinct upper(substring(name, 1, 1)) as name
from blog_tags
union all
select name
from blog_tags
)
order by name
修改强>
如果你想获取应用程序级操作的原始数据,我建议用这种方式查询数据库
select upper(substring(name, 1, 1)) as key,
name
from blog_tags
order by 1, 2
答案 1 :(得分:0)
您可以使用bellow查询来实现此目的,如下所示
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/navigation_drawer_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">
<android.support.design.widget.NavigationView
android:id="@+id/navigation_drawer_top"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@color/menuColor"
android:paddingLeft="50dp"
app:headerLayout="@layout/nav_header_main"
app:itemTextAppearance="@style/NavigationDrawerStyle"
app:itemTextColor="@color/menuTextColour"
app:menu="@menu/menu_navigation_drawer_top"
/>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_drawer_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/menuColor"
android:layout_gravity="bottom"
android:paddingLeft="50dp"
app:itemTextAppearance="@style/NavigationDrawerStyle"
app:itemTextColor="@color/menuTextColour"
app:menu="@menu/menu_navigation_drawer_bottom" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
此查询将按名字对名称进行分组,并将名称以逗号分隔的名称组合在一起。您可以将结果从编程语言转换为数组
输出如下所示
select substr(name,1,1),group_conact(name) from blog_tags group by substr(name,1,1);