Mysql语句及工作中常用语句

创建数据库

        语句: 

CREATE DATABASE rouyi_01 CHARACTER SET utf8 COLLATE utf8_general_ci;

        使用的utf8格式,character set :设置数据的编码格式,collate:数据库校对规则.

创建数据库表

        语句:

CREATE TABLE IF NOT EXISTS Users(
userId INT PRIMARY KEY AUTO_INCREMENT,
userName VARCHAR(50) NOT NULL ,
userPass VARCHAR(20) NOT NULL,
userSex VARCHAR(10) NOT NULL,
userPhone VARCHAR(15) NOT NULL,
userAddr VARCHAR(15) NOT NULL,
userMoney VARCHAR(15) NOT NULL,
busId INT ,
FOREIGN KEY (busId) REFERENCES business(busId)
)ENGINE =INNODB DEFAULT CHARSET=utf8;

        create table 表名(字段,类型,是否为空,[主键],[自增])

重复数据显示只显示一次,使用 

select distinct place from user where city_id=1;

当id不连续时,查找当前此条数据的下一个数据时,EG 1,5,9,20

当查找5后面的9时,用5去查9

select * from news where id = (select id from news where id < 5 order by  id desc limit 1)

当用5去查找1时

select * from news where id = (select id from news where id > 5 order by  id asc limit 1)

group_concat()函数:此函数返回一个字符串结果,

select group_concat(name) from users

 

你可能感兴趣的:(sql,数据库)