2013-03-18 110 views
3

我有以下表格:我需要做些什麼來糾正這個SQL語句?

  1. products - 包含產品庫存。
  2. suppliers - 包含供應商詳細信息。
  3. product_category - 包含產品類別的名稱。
  4. product_suppliers - 包含產品和供應商之間的關係。

這裏最重要的一個可能是,product_suppliers,所以這裏的列:

id //(auto-increment) 
prod_id //(id of product) 
sup_id //(id of supplier) 
preferred //(1 if it's the preferred supplier for that product - 0 if not) 
cost_per_outer //(this suppliers price for this item) 
qty_in_outer //(the number in a box) 

product表,其中存儲產品的詳細信息,也有stock_levelreorder_level的字段。當前者小於後者時,該產品應包括在重新排序列表中。

我想構建列表的查詢,這是不正常工作。

我有4項符合重新排序影響兩個供應商。每個供應商應該有兩個項目,但目前它只是檢索第一個供應商的結果。

我正在使用MySQL和PHP。

我已經運行下面的查詢來給我們一些啓示,以在數據庫中的實際數據:

mysql> SELECT 
    ->   p.prod_id, 
    ->   AES_DECRYPT(p.alt_id, 'MW4KQLg1Irfo3Xz7Nxht')       AS sku, 
    ->   AES_DECRYPT(p.prod_name, 'MW4KQLg1Irfo3Xz7Nxht')      AS prod_name, 
    ->   AES_DECRYPT(p.prod_type, 'MW4KQLg1Irfo3Xz7Nxht')     AS prod_type, 
    ->   AES_DECRYPT(p.stock_level, 'MW4KQLg1Irfo3Xz7Nxht')     AS stock_level, 
    ->   AES_DECRYPT(p.reorder_level, 'MW4KQLg1Irfo3Xz7Nxht')   AS reorder_level, 
    ->   AES_DECRYPT(c.category_name, 'MW4KQLg1Irfo3Xz7Nxht')   AS category_name, 
    ->   ps.sup_id, 
    ->   ps.preferred 
    -> FROM 
    ->   products p 
    -> INNER JOIN 
    ->   product_category c 
    -> ON 
    ->   p.category_id = c.category_id 
    -> INNER JOIN 
    ->   product_supplier ps 
    -> ON 
    ->   p.prod_id = ps.prod_id 
    -> INNER JOIN 
    ->   suppliers s 
    -> ON 
    ->   ps.sup_id = s.supplier_id 
    -> ORDER BY 
    ->   ps.sup_id; 
+---------+------+-----------------+-----------+-------------+---------------+---------------+--------+-----------+ 
| prod_id | sku | prod_name  | prod_type | stock_level | reorder_level | category_name | sup_id | preferred | 
+---------+------+-----------------+-----------+-------------+---------------+---------------+--------+-----------+ 
|  7 | 7 | Term Block  | 1   | 3   | 5    | Electrical |  2 |   1 | 
|  5 | 5 | Electrical Tape | 1   | 12   | 20   | Electrical |  2 |   1 | 
|  6 | 6 | BlowGas   | 1   | 6   | 15   | Plumbing  |  12 |   1 | 
|  1 | 1 | PTFE Tape  | 1   | 9   | 10   | Plumbing  |  12 |   1 | 
+---------+------+-----------------+-----------+-------------+---------------+---------------+--------+-----------+ 

下面是清單,結果只顯示兩個的四個項目我期待的查詢:

mysql> SELECT 
    ->   p.prod_id, 
    ->   AES_DECRYPT(p.alt_id, 'MW4KQLg1Irfo3Xz7Nxht')       AS sku, 
    ->   AES_DECRYPT(p.prod_name, 'MW4KQLg1Irfo3Xz7Nxht')    AS prod_name, 
    ->   AES_DECRYPT(p.prod_desc, 'MW4KQLg1Irfo3Xz7Nxht')     AS prod_desc, 
    ->   AES_DECRYPT(p.stock_level, 'MW4KQLg1Irfo3Xz7Nxht')    AS stock_level, 
    ->   AES_DECRYPT(p.reorder_level, 'MW4KQLg1Irfo3Xz7Nxht')    AS reorder_level, 
    ->   AES_DECRYPT(p.reorder_qty, 'MW4KQLg1Irfo3Xz7Nxht')    AS reorder_qty, 
    ->   p.vat_exempt, 
    ->   AES_DECRYPT(p.lastorderdate, 'MW4KQLg1Irfo3Xz7Nxht')   AS lastorderdate, 
    ->   AES_DECRYPT(p.lastorderqty, 'MW4KQLg1Irfo3Xz7Nxht')    AS lastorderqty, 
    ->   AES_DECRYPT(c.category_name, 'MW4KQLg1Irfo3Xz7Nxht')   AS category_name, 
    ->   ps.sup_id 
    -> FROM 
    ->   products p 
    -> INNER JOIN 
    ->   product_category c 
    -> ON 
    ->   p.category_id = c.category_id 
    -> INNER JOIN 
    ->   product_supplier ps 
    -> ON 
    ->   p.prod_id = ps.prod_id 
    -> INNER JOIN 
    ->   suppliers s 
    -> ON 
    ->   ps.sup_id = s.supplier_id 
    -> WHERE 
    ->   AES_DECRYPT(p.prod_type, 'MW4KQLg1Irfo3Xz7Nxht') = 1 
    -> AND 
    ->   AES_DECRYPT(p.stock_level, 'MW4KQLg1Irfo3Xz7Nxht') <= AES_DECRYPT(p.reorder_level, 'MW4KQLg1Irfo3Xz7Nxht') 
    -> AND 
    ->   ps.preferred = 1 
    -> ORDER BY 
    ->   ps.sup_id; 
+---------+------+-----------------+------------------------+-------------+---------------+-------------+------------+---------------+--------------+---------------+--------+ 
| prod_id | sku | prod_name  | prod_desc    | stock_level | reorder_level | reorder_qty | vat_exempt | lastorderdate | lastorderqty | category_name | sup_id | 
+---------+------+-----------------+------------------------+-------------+---------------+-------------+------------+---------------+--------------+---------------+--------+ 
|  7 | 7 | Term Block  | Nylon connector block. | 3   | 5    | 20   |   0 | NULL   | NULL   | Electrical |  2 | 
|  5 | 5 | Electrical Tape | Black     | 12   | 20   | 100   |   0 | NULL   | NULL   | Electrical |  2 | 
+---------+------+-----------------+------------------------+-------------+---------------+-------------+------------+---------------+--------------+---------------+--------+ 
+1

爲什麼每個列都使用'AES_DECRYPT()'?如果一切都被加密,索引不會很好地工作(如果有的話) – mvp 2013-03-18 05:36:57

+0

http://www.w3resource.com/mysql/encryption-and-compression-functions/aes_decrypt().php:MySQL AES_DECRYPT()函數解密加密的**字符串** ... – MatRt 2013-03-18 05:39:06

+0

因爲這是會計系統的一部分,大多數字段需要加密。如果你有一個更好的解決方案,會很樂意聽到嗎? – 2013-03-18 05:39:37

回答

3

的問題是,你的結果是從AES_DECRYPT()來了來了string型的,這就是爲什麼相比於stock_level和重新排序3 - 512 - 20它顯示的電平值而6 -159 - 20它沒有顯示因爲如果你comapre 6和15串6將超過15同樣是9時和20

希望你有你的問題......

轉換AES_DECRYPT()可在數字前的結果比較..

您的where子句改成這樣

WHERE 
    (AES_DECRYPT(p.prod_type, 'MW4KQLg1Irfo3Xz7Nxht') + 0) = 1 AND 
    (AES_DECRYPT(p.stock_level, 'MW4KQLg1Irfo3Xz7Nxht') + 0) <= (AES_DECRYPT(p.reorder_level, 'MW4KQLg1Irfo3Xz7Nxht') + 0) 

添加+0將結果轉換爲數字

希望這有助於。

0
WHERE 
    ->   AES_DECRYPT(p.prod_type, 'MW4KQLg1Irfo3Xz7Nxht',varchar) = 1 
    -> AND 
    ->   AES_DECRYPT(p.stock_level, 'MW4KQLg1Irfo3Xz7Nxht',varchar) <= AES_DECRYPT(p.reorder_level, 'MW4KQLg1Irfo3Xz7Nxht',varchar) 

或:

WHERE 
    ->   AES_DECRYPT(p.prod_type, 'MW4KQLg1Irfo3Xz7Nxht',int) = 1 
    -> AND 
    ->   AES_DECRYPT(p.stock_level, 'MW4KQLg1Irfo3Xz7Nxht',varchar) <= AES_DECRYPT(p.reorder_level, 'MW4KQLg1Irfo3Xz7Nxht',varchar) 

來自:http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc01015.1510/html/iqsecure/Functions_s_4132405.htm

如果同時插入使用 AES_ENCRYPT函數的數據,你不使用CAST語句,你可以通過VARCHAR作爲數據類型查看使用AES_DECRYPT 功能相同的數據。如果您未將 數據類型傳遞給AES_DECRYPT,則返回VARBINARY數據類型。

+0

謝謝你的嘗試,但我語法錯誤或訪問衝突:1064您的SQL語法錯誤;請查看與您的MySQL服務器版本相對應的手冊,以找到在「int」= 1附近使用的正確語法。 – 2013-03-18 07:30:26