Advertisement
Vita_Harvey

Week 3 notes: Multitable queries

Apr 22nd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.12 KB | None | 0 0
  1. USE ap;
  2.  
  3. /*
  4. Return the names of vendors from Vendors table and the terms from the Terms table,
  5. associated with one another.
  6. */
  7. SELECT
  8.     vendor_name AS Vendor, terms_description AS 'Default Terms'
  9. FROM
  10.     vendors
  11.         JOIN
  12.     terms ON default_terms_id = terms_id
  13. ORDER BY terms_description, vendor_name;
  14.  
  15. SELECT
  16.     vendor_name AS Vendor, terms_description AS 'Default Terms'
  17. FROM
  18.     vendors
  19.         JOIN
  20.     terms ON default_terms_id = terms_id
  21. ORDER BY 'Default Terms', Vendor;
  22.  
  23. SELECT
  24.     vendor_name AS Vendor, invoice_date AS "Date", invoice_number
  25. FROM
  26.     vendors JOIN invoices
  27.     USING(vendor_id)
  28.     WHERE invoice_date LIKE "2014-06%" OR invoice_date LIKE "2014-07%"
  29.             ORDER BY invoice_date DESC, Vendor;
  30.  
  31. # Ambiguous column names;
  32. # Disambiguate by referring to their respective tables.
  33. SELECT
  34.     vendor_name AS Vendor, invoice_date AS "Date", invoice_number, vendors.vendor_id
  35. FROM
  36.     vendors JOIN invoices
  37.     ON vendors.vendor_id = invoices.vendor_id
  38.     WHERE invoice_date LIKE "2014-06%" OR invoice_date LIKE "2014-07%"
  39.             ORDER BY invoice_date DESC, Vendor;
  40.            
  41. # This example uses table aliases (v and i in this example, for Vendors and Invoices,
  42. # respectively).            
  43. SELECT
  44.     vendor_name AS Vendor, invoice_date AS "Date", invoice_number AS "Number", v.vendor_id AS "Vendor ID"
  45. FROM
  46.     vendors v JOIN invoices i
  47.     ON v.vendor_id = i.vendor_id
  48.     WHERE invoice_date LIKE "2014-06%" OR invoice_date LIKE "2014-07%"
  49.             ORDER BY invoice_date DESC, Vendor;
  50.  
  51. # Implicit inner join syntax: deprecated.
  52. SELECT invoice_number, credit_total, terms_description
  53. FROM invoices i, terms t
  54. WHERE i.terms_id = t.terms_id; 
  55.  
  56. # Fucking around.            
  57. SELECT terms_description, t.terms_id, i.terms_id
  58. FROM terms t
  59.     JOIN invoices i USING(terms_id)
  60.         WHERE i.terms_id > 5;
  61.  
  62. # Example of Left Outer Join        
  63. SELECT vendor_name, account_number, account_description
  64. FROM vendors v
  65.             LEFT JOIN general_ledger_accounts g
  66.                 ON v.default_account_number = g.account_number
  67.                 WHERE account_description NOT LIKE 'State Corporation Incomes Taxes'
  68.                 ORDER BY account_description, vendor_name DESC;
  69.                
  70. # Example of a self-join.
  71. # One table is aliased so as to treat it like two different tables.
  72. USE ex;
  73. # SELECT fields to work with...
  74. SELECT CONCAT(emps.last_name, ", ", emps.first_name) AS subordinate, CONCAT(mgrs.last_name, ", ", mgrs.first_name) AS boss
  75. # ....FROM employees table....
  76. FROM employees emps
  77.     # ...JOIN mgrs part of employees...
  78.     JOIN employees mgrs
  79.     # ....on the basis of the value of manager_id of employee = employee_id of the manager.
  80.     ON emps.manager_id = mgrs.employee_id;
  81.  
  82. # 3-table join example.
  83. USE ap;    
  84. SELECT DISTINCT vendor_name, line_item_description
  85. FROM vendors
  86.     JOIN invoices USING(vendor_id)
  87.     JOIN invoice_line_items USING(invoice_id)
  88.     WHERE vendor_name NOT LIKE "Postmaster%" OR vendor_name NOT LIKE "United Parcel Service%"
  89.     ORDER BY vendor_name DESC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement