VbzCart/queries

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< VbzCart
Revision as of 16:46, 2 November 2007 by Woozle (talk | contribs) (→‎qryCbx_Items: working version (refinements later))
Jump to navigation Jump to search

Navigation

VbzCart: data views

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 AS SELECT

 i.ID,
 CAST(
   CONCAT(
     if(i.isPulled,
       CONCAT('!X '),
       
     ),
     if(i.CatNum IS NULL,
       CONCAT('#',i.ID,' (no cat#)')
       ,i.CatNum),
     if(i.Supp_CatNum IS NULL,
       ,
       CONCAT(' [',i.Supp_CatNum,']')
     ),
     if(i.isForSale,,' *n/a*'),
     ' ',
     if(i.Descr IS NULL,'no description',i.Descr)
   ) AS CHAR
 ) AS Descr,
 i.isForSale

FROM qryCat_Items AS i ORDER BY NOT i.isPulled, i.isForSale, i.CatNum;</mysql>

  • Future refinement: no need to display CatNum, Supp_CatNum, i.Descr if i.isPulled

qryCbx_Orders

<mysql>CREATE OR REPLACE VIEW qryCbx_Orders AS SELECT

 ID,
 CONCAT_WS(' due ',CAST(Number AS CHAR),DATE_FORMAT(WhenNeeded,'%Y-%m-%d')) AS Descr

FROM core_orders ORDER BY Number DESC;</mysql>

qryCbx_Restocks

I don't know WTF is going on here. MySQL wouldn't let me create this as one big query (no error message, just "query cancelled"), so I tried to split it into two parts and union them -- but I get the same error message. For now, I'm just defining the subqueries in MySQL and the UNION in Access. :-P <mysql>CREATE OR REPLACE VIEW qryCbx_Restocks_byStatus AS SELECT

 r.ID,
 CAST(
   CONCAT(
     CAST(COALESCE(
       CONCAT('X ',DATE_FORMAT(r.WhenKilled,'%Y-%m-%d')),
       CONCAT('R ',DATE_FORMAT(r.WhenReceived,'%Y-%m-%d')),
       CONCAT('O ',DATE_FORMAT(r.WhenOrdered,'%Y-%m-%d')),
       CONCAT('D ',DATE_FORMAT(r.WhenDebited,'%Y-%m-%d')),
       CONCAT('C ',DATE_FORMAT(r.WhenCreated,'%Y-%m-%d'))
     ) AS BINARY),
     ' ',
     s.CatKey,
     IFNULL(CONCAT(' [',r.PurchOrdNum,']'),)
   ) AS CHAR
 ) AS Descr,
 NOT IsLocked AS IsOpen,
 r.WhenOrdered,
 r.WhenReceived,
 r.WhenConfirmed,
 r.ID_Supplier

FROM core_restocks AS r LEFT JOIN cat_supp AS s ON r.ID_Supplier=s.ID ORDER BY COALESCE(r.WhenKilled,r.WhenReceived,r.WhenOrdered,r.WhenDebited,r.WhenCreated);

CREATE OR REPLACE VIEW qryCbx_Restocks_byPurchOrd AS SELECT

 r.ID,
 CAST(r.PurchOrdNum AS CHAR) AS Descr,
 NOT IsLocked AS IsOpen,
 r.WhenOrdered,
 r.WhenReceived,
 r.WhenConfirmed,
 r.ID_Supplier

FROM core_restocks AS r WHERE r.PurchOrdNum IS NOT NULL ORDER BY r.PurchOrdNum;

CREATE OR REPLACE VIEW qryCbx_Restocks AS

 SELECT * FROM qryCbx_Restocks_byStatus

UNION

 SELECT * FROM qryCbx_Restocks_byPurchOrd;</mysql>