sql之left join、right join、inner join的区别
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
inner join(等值连接) 只返回两个表中联结字段相等的行
举例如下:
--------------------------------------------
表A记录如下:
aID aNum
1 a20050111
2 a20050112
3 a20050113
4 a20050114
5 a20050115
表B记录如下:
bID bName
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408
--------------------------------------------
1.left join
sql语句如下:
select * from A
left join B
on A.aID = B.bID
结果如下:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
5 a20050115 ......
数据库 有两张表
表1: student
表2:chinese
现在要分别列出 每所学校 语文成绩最高的 学生信息
SQL :
SELECT *
from student
LEFT JOIN chinese ON student.no = chinese.no
WHERE chinese.chengji
IN (
SELECT max( chinese.chengji )
from student
LEFT JOIN chinese ON student.no = chinese.no
GROUP BY student.school
ORDER BY chinese.chengji DESC
)
LIMIT 0 ,30
执行结果:
PS: SQL 语句也是在学习中。可能写的也不是很好 贴上来方便像我这样的菜鸟借鉴一下 如有更好的方式 希望回复告诉我一下。本人将万分感激。 ......
如果你使用的是 SQL Server 2008, 当你修改数据结构后,保存时会报下图情况: Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created. 这是 SQL Server 2008 的一种自我保护,当你的修改可能导致数据表被删除并重新创建时(dropped and re-created),就会有这个提醒。 导致表被重新创建可能是以下几种情况: 在表中间添加一个新列; 删除列 更改列为 Null 性 更改列的顺序 更改列的数据类型 若要不使用这个保护,需要在“工具”菜单中单击“选项”,展开“设计器”,然后单击“表设计器和数据库设计器”。 清除“阻止保存要求重新创建表的更改”复选框。 on the Tools menu, click Options, expand Designers, and then click Table and Database Designers. Select or clear the Prevent saving changes that require the table to be re-created check box. 如下图: 参考资料: Save (Not Permitted) ......
怎样从数据库里随机读取
一条记录,
SELECT TOP 1 * from dbo.Customers ORDER BY NEWID()
这样,如果是随机10
条,100条。。。。
SELECT TOP 10 * from dbo.Customers ORDER BY NEWID()
很简单吧。
不过top后面数字越大,运行速度越慢。不推荐数据字太大。
以后代码在SQL20005中测试通过,ACCESS好像不行。
......
to_date和to_char是oracle里里面的内置函数而不是标准的sql语法中的函数,用法举例:
1.to_char,返回结果可显示为各种形式
select to_char(sysdate,'yyyy/mm/dd') ,sysdate from dual;
结果: 2010/05/26 2010-5-26 17:09:33
2.to_date,转化为DATE型,返回结果形式只有一种
select to_date( '21/01/2002 18:02:23 ' , 'dd/mm/yyyy hh24:mi:ss' ),to_date( '21/01/2002 8:02:23 ' , 'dd/mm/yyyy hh:mi:ss PM' ) from dual;
结果: 2002-1-21 18:02:23 2002-1-21 8:02:23
......
使用SQL Server的朋友們應該都知道SQL Server的資料庫有一個設定叫做定序(Collation),今天我們就來看看定序這東西是什麼,首先我們看一下Wiki上對定序的說明:
Collation is the assembly of written information into a standard order. One common type of collation is called alphabetisation, though collation is not limited to ordering letters of the alphabet. Collating lists of words or names into alphabetical order is the basis of most office filing systems, library catalogs and reference books..[Reference from here.]
這段話講得玄了點,我們看另一個比較易懂的說明:
Collation refers to a set of rules that determine how data is sorted and compared. Character data is sorted using rules that define the correct character sequence, with options for specifying case-sensitivity, accent marks, kana character types and character width.[Reference from here.]
這段 ......