MySQL常用操作分栏整理(二)

MySQL常用操作分栏整理(二)

1.使用索引

索引是加快查询的最重要的工具。没有索引,MySQL不得不首先以第一条记录开始并然后读完整个表直到它 找出相关的行(全表扫描)。表越大,花费时间越多。 使用索引消除了全表扫描,极大地加快了搜索的速度。在执行涉及多个表 的连接查询时,索引甚至会更有价值。
最适合索引的列是出现在 WHERE 子句中的列,或连接子句中指定的列。
不适合使用索引的场合:模糊查询、使用了IS NOT NULL、<>比较运算符的场合、对列使用了运算/函数的场合、复合索引的第一列没被包含在where条件语句中的场合等,即索引在降低性能时,不适用索引。
在使用过程中,使用惟一索引和短索引。

  1. 创建索引 create index 索引名字 on 表名(字段);
  2. 查看索引 show index from 表名;
  3. 删除索引 drop index 索引名字 on 表名;

例子:
(fruits的创建见MySQL常用操作分栏整理(一))
为fruits的f_price字段使用索引:
创建索引 create index idx_price on fruits(f_price);
查看索引 show index from fruits;
删除指定索引 drop index idx_price on fruits;

2.多表连接查询(重点)

在进行多表连接查询中,比较少的数据放左边。有重复值时需要把所有的连接情况体现出来。(重复值处理这一点尤其重要)

  1. 内连接查询 – 内连接(inner join),进行表间某些列数据的比较操作,并列出这些表 与连接条件相匹配的数据行,组合成新的记录 。
    select 表1中的字段,表2中的字段 from 表1,表2 where 条件;
    select 表1中的字段,表2中的字段 from 表1 别名 inner join表2 别名on 连接的条件 where 判断的条件;
    当不同的表中的字段名称相同时,用表名.字段。
  2. 外连接查询 – 有时需要包含没有关联到的行数据,即返回的查询结果集合中不仅包含符合连接条件的行,而且还包括左表、右表、或两个连接表中 的所有数据行。
    – left join:左连接,左边连接表中的所有记录都查询出来。
    – right join:右连接,右边连接表中的所有记录都查询出来。
  3. 复合条件连接查询 – 复合条件连接查询是在连接查询的过程中,通过添加过滤条件,限 制查询的结果,使查询的结果更加准确。
    例子:
    – 查询’apple’这个商品的供货商信息
    select f_name,f_price,fruits.s_id,s_name,s_city from fruits,suppliers where f_name=‘apple’ and fruits.s_id=suppliers.s_id;
    使用别名(可以加as 也可不加as)
    select f_name,f_price,f.s_id,s_name,s_city from fruits f,suppliers s where f_name=‘apple’ and f.s_id=s.s_id;
    ANSI SQL语法:内连接(推荐使用这种方法)内连接 inner join也可以省略join
    select f_name,f_price,f.s_id,s_name,s_city from fruits f inner join suppliers s on f.s_id=s.s_id where f_name=‘apple’;
    – 查询所有商品的供货商信息
    select f_name,f_price,f.s_id,s_name,s_city from fruits f inner join suppliers s on f.s_id=s.s_id;
    – 查询所有商品的供货商信息,包括未供应商品的供应商信息(供应商在右边,所有为右连接)
    select f_name,f_price,f.s_id,s_name,s_city from fruits f right outer join suppliers s on f.s_id=s.s_id;

3.子查询

子查询为一个查询语句嵌套在另外一个查询语句中的查询。一般情况,先执行子查询,再执行外部查询。查询可以基于一个表或者多个表。
常用操作符:any(some) 查询结果满足一个就可,all满足所有,in, exist。
例子:查询所有价格高于平均价格的商品信息
select * from fruits where f_price>(select avg(f_price) from fruits);
查询所有来自北京市供应商的商品信息
select * from fruits where s_id in (select s_id from suppliers where s_city=‘北京市’);
合并查询结果:可以将多个查询结果合并到一个表中(union, union all)union会将两个查询结果的重复项删除,union all 不会删除两个查询结果的重复项,出现几次即为几次。
A查询结果 union B查询结果;

4.正则表达式查询

正则表达式的关键字:regexp
^ 匹配文本的开始字符,以什么开始
$ 匹配文本的结束字符,以什么结束
. 匹配任意单个字符
(x) 匹配包含指定字符x的字符
* 前面的字符重复的次数零次或多次
+ 前面的字符重复的次数一次或多次,至少一次
{n} 前面的字符出现n次
{n,} 前面的字符至少出现n次
{n,m} 前面的字符至少出现n次,至多出现m次

例子:
– 在fruits表中查询所有名称以a开头的 商品
select * from fruits where f_name regexp ‘^a’;
– 在fruits表中查询所有名称中含有x的 商品
select * from fruits where f_name regexp ‘x’;

----------------------------总结
以下是我收集到的MySQL的常用的操作的一些PDF,言简意赅,非常容易上手,有需要的自取。
百度云盘:https://pan.baidu.com/s/1oUPlTC1fhgITq2cpv3cKjA
提取码:2rlr

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个 ExpandableListView 分栏的例子: 首先在布局文件中定义 ExpandableListView 和 Adapter: ```xml <ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="match_parent"/> <!-- 定义自定义的 ExpandableListAdapter --> ``` 然后在 Activity 中设置 Adapter 并为 ExpandableListView 设置点击事件: ```java public class MainActivity extends AppCompatActivity { private ExpandableListView expandableListView; private ExpandableListAdapter adapter; private List<String> listDataHeader; private HashMap<String, List<String>> listDataChild; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 准备数据 prepareListData(); // 设置 Adapter adapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); expandableListView.setAdapter(adapter); // 设置点击事件 expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(getApplicationContext(), listDataHeader.get(groupPosition) + " : " + listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show(); return false; } }); } // 准备数据 private void prepareListData() { listDataHeader = new ArrayList<>(); listDataChild = new HashMap<>(); // 添加一级分栏 listDataHeader.add("Fruits"); listDataHeader.add("Vegetables"); // 添加分栏 List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Mango"); List<String> vegetables = new ArrayList<>(); vegetables.add("Carrot"); vegetables.add("Tomato"); vegetables.add("Potato"); // 将分栏添加到对应的一级分栏下 listDataChild.put(listDataHeader.get(0), fruits); listDataChild.put(listDataHeader.get(1), vegetables); } } ``` 最后是 ExpandableListAdapter 的实现: ```java public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context context; private List<String> listDataHeader; private HashMap<String, List<String>> listDataChild; public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) { this.context = context; this.listDataHeader = listDataHeader; this.listDataChild = listDataChild; } @Override public int getGroupCount() { return this.listDataHeader.size(); } @Override public int getChildrenCount(int groupPosition) { return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return this.listDataHeader.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_group, null); } TextView lblListHeader = convertView.findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_item, null); } TextView txtListChild = convertView.findViewById(R.id.lblListItem); txtListChild.setText(childText); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 其中 `list_group.xml` 和 `list_item.xml` 分别是一级分栏分栏的布局文件,可以根据需要自行定义。 以上就是 ExpandableListView 分栏的例子,希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值