Use a derived table. : Sub query « Select Clause « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Select Clause » Sub query 
Use a derived table.
       
mysql>
mysql> CREATE TABLE rel_title_author (
    ->   titleID int(11),
    ->   authID int(11),
    ->   ts timestamp ,
    ->   PRIMARY KEY  (titleID,authID)
    -> );
Query OK, rows affected (0.00 sec)

mysql>
mysql>
mysql>
mysql> INSERT INTO rel_title_author VALUES (1,1,'2004-12-02 18:37:09'),
    ->                                       (2,1,'2004-12-02 18:37:09'),
    ->                                       (2,2,'2004-12-02 18:37:09'),
    ->                                       (3,3,'2005-02-24 10:46:37'),
    ->                                       (3,4,'2005-02-24 10:46:37'),
    ->                                       (3,5,'2005-02-24 10:46:37'),
    ->                                       (4,6,'2004-12-02 18:37:09');
Query OK, rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> CREATE TABLE titles (
    ->   titleID int(11),
    ->   title varchar(100),
    ->   subtitle varchar(100),
    ->   edition tinyint(4),
    ->   publID int(11),
    ->   catID int(11),
    ->   langID int(11),
    ->   year int(11),
    ->   isbn varchar(20),
    ->   comment varchar(255),
    ->   ts timestamp,
    ->   authors varchar(255),
    ->   PRIMARY KEY  (titleID)
    -> );
Query OK, rows affected (0.00 sec)

mysql>
mysql>
mysql>
mysql> INSERT INTO titles VALUES (1,'Linux','Installation',5,1,57,2,2000,NULL,NULL,'2005-02-28 13:34:21','Michael'),
    ->                           (2,'Excel',NULL,NULL,2,3,NULL,2000,NULL,NULL,'2005-02-28 13:34:22','David'),
    ->                           (3,'XML',NULL,NULL,1,2,NULL,1997,NULL,NULL,'2005-02-28 13:34:22','Edwards'),
    ->                           (4,'PHP',NULL,NULL,3,6,NULL,2000,NULL,NULL,'2005-02-28 13:34:22','Tom'),
    ->                           (5,'MySQL','',0,3,34,NULL,2000,'','','2005-02-28 13:34:22','Paul'),
    ->                           (6,'Java',NULL,NULL,4,34,NULL,1999,NULL,NULL,'2005-02-28 13:34:22','Tim');
Query OK, rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql>
mysql> SELECT FROM
    -> (SELECT titles.titleID, title, COUNT(authIDAS authCount
    -> FROM titles, rel_title_author
    -> WHERE titles.titleID = rel_title_author.titleID
    -> GROUP BY rel_title_author.titleID
    -> HAVING authCount>1AS titleAuthCount
    -> ORDER BY authCount DESC;
+---------+-------+-----------+
| titleID | title | authCount |
+---------+-------+-----------+
|       | XML   |         |
|       | Excel |         |
+---------+-------+-----------+
rows in set (0.00 sec)

mysql>
mysql>
mysql> drop table titles;
Query OK, rows affected (0.00 sec)

mysql> drop table rel_title_author;
Query OK, rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
    
  
Related examples in the same category
1.Subqueries As Calculated Columns: Simple Subqueries
2.Subqueries in the WHERE Clause 2
3.Subqueries That Return Multiple Results
4.Sub queries
5.Sub query with string concatenate
6.Sub query with not equal and order
7.Sub query with calculation
8.Update command with sub query
9.Delete command with sub query
10.SubSELECTs Syntax Variants
11.Uses a subquery to return an AuthID value
12.Use a not equal (<>) comparison operator in the WHERE clause to introduce the subquery
13.Subquery uses an aggregate function to arrive at a value that the outer statement can use
14.Nested subquery
15.Four-level nested subquery with alias
16.Nested subquery and where clause
17.Minus value from subquery
18.Subquery and equals operator(ERROR 1242 (21000): Subquery returns more than 1 row)
19.Row values and subquery
20.Nested three level subquery
21.Greater than the result of subquery
22.Equals to the result of subquery
23.Row values comparison for subquery
24.Select from the result of subquery
25.Value calculation of subquery
26.Subquery as a table
27.Alias subquery
28.Calculation in subquery
29.Using function with result from subquery
30.Create constant value in subquery
31.Calculation in subquery and use the result from outside
32.Equals operator with subquery
33.Less than and subquery(ERROR 1242 (21000): Subquery returns more than 1 row)
34.Less than data type value from subquery
35.<=> and subquery
36.Less than two values in subquery
37.Greater than two values in subquery
38.Get substring for a value from subquery
39.Mix constant value and subquery
40.Using the value from subquery with in operator
41.Pair value within in operator and subquery
42.In a list of value from subquery
43.In operator and subquery
44.Nested subquery and in operator
45.Not in and subquery
46.Between...and opertor and subquery
47.Count value in subquery
48.Add up count value from subquery
49.From a subquery
50.Aggregate function in subquery
51.Having clause with subquery
52.Count and subquery in Having clause
53.Subquery with having clause
54.Order by value from subquery
55.Subquery with limit clause
56.Insert statement with subquery
57.Searching for Titles Without Authors
58.Compare to aggregate function
59.Data calculation inside in operator
60.COMPARISON OPERATORS WITH SUBQUERIES
61.Select from three subqueries
62.Select from two subqueries
63.Row value comparison with select statement
64.Compare two data type value together
65.Nested subqueries
66.Two USING clauses, rather than ON clauses
67.Finding people who didn't send mail to themselves
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.