943,548 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 1645
  • MySQL RSS
Aug 7th, 2008
0

combine three tables

Expand Post »
hi
how to combine three tables in a single query.please send a sample code
thanks
Similar Threads
Reputation Points: 19
Solved Threads: 5
Junior Poster
lydia21 is offline Offline
183 posts
since Nov 2007
Aug 7th, 2008
-1

Re: combine three tables

Check out the Referring to Two Tables example
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Aug 8th, 2008
0

Re: combine three tables

this is one example:
this is based on joins...
mysql Syntax (Toggle Plain Text)
  1. CREATE TABLE Articles (
  2. ArticleID SMALLINT NOT NULL PRIMARY KEY,
  3. ArticleTitle VARCHAR(60) NOT NULL,
  4. Copyright YEAR NOT NULL
  5. )
  6. ENGINE=INNODB;
  7.  
  8.  
  9. INSERT INTO Articles VALUES (12786, 'How write a paper', 1934),
  10. (13331, 'Publish a paper', 1919),
  11. (14356, 'Sell a paper', 1966),
  12. (15729, 'Buy a paper', 1932),
  13. (16284, 'Conferences', 1996),
  14. (17695, 'Journal', 1980),
  15. (19264, 'Information', 1992),
  16. (19354, 'AI', 1993);
  17.  
  18.  
  19. CREATE TABLE Authors (
  20. AuthID SMALLINT NOT NULL PRIMARY KEY,
  21. AuthorFirstName VARCHAR(20),
  22. AuthorMiddleName VARCHAR(20),
  23. AuthorLastName VARCHAR(20)
  24. )
  25. ENGINE=INNODB;
  26.  
  27.  
  28. INSERT INTO Authors VALUES (1006, 'Henry', 'S.', 'Thompson'),
  29. (1007, 'Jason', 'Carol', 'Oak'),
  30. (1008, 'James', NULL, 'Elk'),
  31. (1009, 'Tom', 'M', 'Ride'),
  32. (1010, 'Jack', 'K', 'Ken'),
  33. (1011, 'Mary', 'G.', 'Lee'),
  34. (1012, 'Annie', NULL, 'Peng'),
  35. (1013, 'Alan', NULL, 'Wang'),
  36. (1014, 'Nelson', NULL, 'Yin');
  37.  
  38.  
  39. CREATE TABLE AuthorArticle (
  40. AuthID SMALLINT NOT NULL,
  41. ArticleID SMALLINT NOT NULL,
  42. PRIMARY KEY (AuthID, ArticleID),
  43. FOREIGN KEY (AuthID) REFERENCES Authors (AuthID),
  44. FOREIGN KEY (ArticleID) REFERENCES Articles (ArticleID)
  45. )
  46. ENGINE=INNODB;
  47.  
  48.  
  49. INSERT INTO AuthorArticle VALUES (1006, 14356),
  50. (1008, 15729),
  51. (1009, 12786),
  52. (1010, 17695),
  53. (1011, 15729),
  54. (1012, 19264),
  55. (1012, 19354),
  56. (1014, 16284);
  57.  
  58. SELECT STRAIGHT_JOIN ArticleTitle, Copyright,
  59. CONCAT_WS(' ', AuthorFirstName, AuthorMiddleName, AuthorLastName) AS Author
  60. FROM Articles AS b, AuthorArticle AS ab, Authors AS a
  61. WHERE b.ArticleID=ab.ArticleID AND ab.AuthID=a.AuthID AND Copyright<1980
  62. ORDER BY ArticleTitle;
see this for reference:
http://www.tizag.com/mysqlTutorial/mysqljoins.php
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: insert checkbox values to database
Next Thread in MySQL Forum Timeline: In Mysql Storing and retriving the image with asp.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC