首页 Soft PlugIn RAN乱 Dev开发 Info资料 English WAP 留言 登陆 注册
-
Posted by Yippee | 评论(0) | 引用(1) | 阅读1823次
iwas7-mysql subqueries

真是莫名其妙,忘了拷贝原来那条SQL语句了
select namee from param where namee in (select vall from param where namee = 'style')

执行一条子查询 嵌套查询 SQL语句,结果返回
MySQL 返回:  www.shengfang.org

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select namee from param where namee = 'style' )  LIMIT 0, 30' at

这是ACCESS翻版:select * from aa where a in (select b from aa where a='a1')

大找资料: www.shengfang.org

SELECT C.CUST_NBR, C.NAME
FROM CUSTOMER C
WHERE C.CUST_NBR IN (SELECT O.CUST_NBR
FROM CUST_ORDER O, EMPLOYEE E
WHERE O.SALES_EMP_ID = E.EMP_ID
AND E.LNAME = 'MARTIN');

在select查询语句里可以嵌入select查询语句,称为嵌套查询。有些书上将内嵌的select语句称为子查询,子查询形成的结果又成为父查询的条件。 www.shengfang.org
子查询可以嵌套多层,子查询操作的数据表可以是父查询不操作的数据表。子查询中不能有order by分组语句。

查询是使用最多的SQL命令。查询数据库需要凭借结构、索引和字段类型等因素。大多数数据库含有一个优化器(optimizer),把用户的查  询语句转换成可选的形式,以提高查询效率。
  值得注意的是MySQL不支持SQL92标准的嵌套的where子句,即它只支持一个where子句。

Subqueries www.shengfang.org
如果低于4.1的 请升级mysql !
低版本的mysql 无法进行子查询的

我的版本就是4.1了~MySQL 4.1.13a-nt

SELECT *
FROM param
WHERE vall = (
SELECT namee
FROM param )
LIMIT 0 , 30
MySQL 返回:  www.shengfang.org

#1242 - Subquery returns more than 1 row

SELECT *
FROM param
WHERE vall
IN (

SELECT namee
FROM param
) www.shengfang.org

在帮助里面看到一个例子:
13.2.8. Subquery Syntax
A subquery is a SELECT statement inside another statement.

Starting with MySQL 4.1, all subquery forms and operations that the SQL standard requires are supported, as well as a few features that are MySQL-specific.

With MySQL versions prior to 4.1, it was necessary to work around or avoid the use of subqueries. In many cases, subqueries can successfully be rewritten using joins and other methods. See Section 13.2.8.11, “Rewriting Subqueries as Joins for Earlier MySQL Versions”.

Here is an example of a subquery:

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

In this example, SELECT * FROM t1 ... is the outer query (or outer statement), and (SELECT column1 FROM t2) is the subquery. We say that the subquery is nested in the outer query, and in fact it's possible to nest subqueries within other subqueries, to a great depth. A subquery must always appear within parentheses.

The main advantages of subqueries are:

They allow queries that are structured so that it's possible to isolate each part of a statement.

They provide alternative ways to perform operations that would otherwise require complex joins and unions.

They are, in many people's opinion, readable. Indeed, it was the innovation of subqueries that gave people the original idea of calling the early SQL ``Structured Query Language.''

www.shengfang.org


字体:

Permanant URI永久地址 http://www.shengfang.org/blog/p/iwas7mysqlsubq.php
Trackback URI引用地址 http://www.shengfang.org/blog/tb.php?tb_id=1124857363

2005年8月24日12:22星期三  [Dev开发] 追踪此文的RSS
提示:
此文还没有评论。

称呼:    登陆   注册
   不注册,但记住我的信息
邮件:
(非必须)
评论: [UBB代码帮助]
粗体 斜体 下划线 链接 水平线 引用



验证码: 请输入你看见的数字
关闭UBB      提交时自动将内容复制到剪贴板

公告





统计信息
[Yippee]||[统计]||日志:2143
在线: 6||用户: 2635 [列表]
今日:884||到访:3654669
Rss:||评论:1668

最新日志

最新评论

友情链接

日历
342009 - 0734
   1234
567891011
12131415161718
19202122232425
262728293031 

最新引用
搜索

归档

杂项
Get RSS Feed (Version 2.0)
Get Atom Feed (Version 0.3)
编码:  UTF-8