Difference between revisions of "VbzCart/queries"
< VbzCart
Jump to navigation
Jump to search
(→qryCat_Items: found the problem -- function usage syntax) |
(→Views: creation code for _items; changed sql dialect for highlighting to mysql) |
||
Line 8: | Line 8: | ||
* '''qryCat_''': queries which build catalog numbers or information by joining multiple tables. (In Access, the convention was '''qryCatNum_'''.) | * '''qryCat_''': queries which build catalog numbers or information by joining multiple tables. (In Access, the convention was '''qryCatNum_'''.) | ||
===qryCat_Depts=== | ===qryCat_Depts=== | ||
− | <mysql> | + | <mysql>CREATE OR REPLACE VIEW qryCat_Depts AS |
− | CREATE OR REPLACE VIEW qryCat_Depts AS | ||
SELECT | SELECT | ||
d.ID, | d.ID, | ||
Line 19: | Line 18: | ||
FROM depts AS d LEFT JOIN suppliers AS s ON d.ID_Supplier=s.ID;</mysql> | FROM depts AS d LEFT JOIN suppliers AS s ON d.ID_Supplier=s.ID;</mysql> | ||
===qryCat_Titles=== | ===qryCat_Titles=== | ||
− | < | + | <mysql>CREATE OR REPLACE VIEW qryCat_Titles AS |
SELECT | SELECT | ||
t.ID, | t.ID, | ||
Line 30: | Line 29: | ||
t.Notes, | t.Notes, | ||
t.Supplier_CatNum AS Supp_CatNum | t.Supplier_CatNum AS Supp_CatNum | ||
− | FROM titles AS t LEFT JOIN qryCat_Depts AS d ON t.ID_Dept=d.ID;</ | + | FROM titles AS t LEFT JOIN qryCat_Depts AS d ON t.ID_Dept=d.ID;</mysql> |
===qryCat_Items=== | ===qryCat_Items=== | ||
− | < | + | <mysql>CREATE OR REPLACE VIEW qryCat_Items AS SELECT |
i.ID, | i.ID, | ||
i.ID_Title, | i.ID_Title, | ||
Line 59: | Line 58: | ||
t.QtyMin_Rstk AS QtyMin_Rstk_Title | t.QtyMin_Rstk AS QtyMin_Rstk_Title | ||
FROM cat_items AS i LEFT JOIN qryCat_Titles AS t on i.ID_Title=t.ID | FROM cat_items AS i LEFT JOIN qryCat_Titles AS t on i.ID_Title=t.ID | ||
− | ORDER BY t.ID IS NOT NULL, i.CatNum, i.ItOpt_Sort;</ | + | ORDER BY t.ID IS NOT NULL, i.CatNum, i.ItOpt_Sort;</mysql> |
* '''MySQL note''': Functions (or CONCAT, anyway) apparently do not like being separated from their opening parenthesis; it seems to resolve the function name as a field name, and (generally) will report that there is no such field. | * '''MySQL note''': Functions (or CONCAT, anyway) apparently do not like being separated from their opening parenthesis; it seems to resolve the function name as a field name, and (generally) will report that there is no such field. | ||
===qryCbx_Items=== | ===qryCbx_Items=== | ||
− | < | + | <mysql>CREATE OR REPLACE VIEW qryCbx_Items ASSELECT |
i.ID, | i.ID, | ||
CONCAT( | CONCAT( | ||
Line 75: | Line 74: | ||
i.isForSale | i.isForSale | ||
FROM qryCat_Items AS i | FROM qryCat_Items AS i | ||
− | ORDER BY NOT i.isPulled, i.isForSale, i.CatNum;</ | + | ORDER BY NOT i.isPulled, i.isForSale, i.CatNum;</mysql> |
Revision as of 10:51, 4 October 2007
Overview
"Views" in MySQL are like SELECT "queries" in MS Access, i.e. they pull data from existing tables and are themselves usable as data sources in much the same way that tables are (and in which result sets from functions are not).
Views
Some common prefixes:
- qryCbx_: queries used for filling comboboxes. The unique ID will always be the first field, and the text to display will always be the second field; additional fields may be provided for use in further qryCbx_ queries which show a subset of the results.
- qryCat_: queries which build catalog numbers or information by joining multiple tables. (In Access, the convention was qryCatNum_.)
qryCat_Depts
<mysql>CREATE OR REPLACE VIEW qryCat_Depts AS
SELECT d.ID, d.Name, d.Sort, d.CatKey, d.ID_Supplier, CONCAT_WS('-',s.CatKey,d.CatKey) AS CatNum FROM depts AS d LEFT JOIN suppliers AS s ON d.ID_Supplier=s.ID;</mysql>
qryCat_Titles
<mysql>CREATE OR REPLACE VIEW qryCat_Titles AS
SELECT t.ID, t.Name, CONCAT_WS('-',d.CatNum,t.CatKey) AS CatNum, d.ID_Supplier, t.ID_Dept, t.DateAdded, t.RstkMin AS QtyMin_Rstk, t.Notes, t.Supplier_CatNum AS Supp_CatNum FROM titles AS t LEFT JOIN qryCat_Depts AS d ON t.ID_Dept=d.ID;</mysql>
qryCat_Items
<mysql>CREATE OR REPLACE VIEW qryCat_Items AS SELECT
i.ID, i.ID_Title, (t.ID IS NOT NULL) AS TitleExists, t.ID_Supplier, i.PriceSell, i.PriceList, i.PriceBuy, i.CatNum, t.CatNum AS Title_CatNum, t.Name AS Title_Name, i.ID_ItTyp, i.ID_ItOpt, i.ItOpt_Descr, i.ItOpt_Sort, CONCAT(t.Name, IF (i.ItOpt_Descr IS NULL,,CONCAT( ' (',i.ItOpt_Descr,')' ) ) ) AS Descr, i.Supp_CatNum, i.isForSale, i.isPulled, i.QtyMin_Stk, t.QtyMin_Rstk AS QtyMin_Rstk_Title
FROM cat_items AS i LEFT JOIN qryCat_Titles AS t on i.ID_Title=t.ID ORDER BY t.ID IS NOT NULL, i.CatNum, i.ItOpt_Sort;</mysql>
- MySQL note: Functions (or CONCAT, anyway) apparently do not like being separated from their opening parenthesis; it seems to resolve the function name as a field name, and (generally) will report that there is no such field.
qryCbx_Items
<mysql>CREATE OR REPLACE VIEW qryCbx_Items ASSELECT
i.ID, CONCAT( IF(i.isPulled,CONCAT('!PULLED (',i.ID,')'),), i.CatNum, IF(i.Supp_CatNum IS NULL,,CONCAT('[',i.SuppCatNum,']')), IF(i.isForSale,,' n/a'), i.Descr) AS Descr, i.isForSale
FROM qryCat_Items AS i ORDER BY NOT i.isPulled, i.isForSale, i.CatNum;</mysql>