Database Articles
  Home arrow Database Articles arrow Page 2 - Writing UNION statements in MySQL 3.x
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE ARTICLES

Writing UNION statements in MySQL 3.x
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-12-05

    Table of Contents:
  • Writing UNION statements in MySQL 3.x
  • Overview
  • The Details
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Writing UNION statements in MySQL 3.x - Overview


    (Page 2 of 4 )

    The secret lies in the use of LEFT JOIN's and a dummy table. A LEFT JOIN is similar to an INNER JOIN (which is the standard type of join), except that even if the join requirements aren't met, a record is returned, but the fields from the joined table will be NULL.

    The dummy table is a table that must be created in the database with only one field. I call the table _dummy and I call the field num. The table simply contains a different number in each record, starting at zero. You only need as many records as UNION's you plan to use. For example, if you only need to join two SELECT's, you only need 0 and 1 in _dummy. If you plan to join four SELECT's, then you need 0 through 3. This dummy table will allow you to make separate queries within one query.

    Here's the basic overview of how it's done:

    SELECT [fields]
    FROM _dummy AS D
    LEFT JOIN [table1] ON (D.num = 0 AND [condition1])
    LEFT JOIN [table2] ON (D.num = 1 AND [condition2])
    .
    .
    .
    WHERE D.num < [table count]

    More Database Articles Articles
    More By Codewalkers


     

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT