3

我有以下表格:

  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_level和的字段reorder_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 |
+---------+------+-----------------+------------------------+-------------+---------------+-------------+------------+---------------+--------------+---------------+--------+
4

2 回答 2

3

问题是您的结果来自 AES_DECRYPT() 的string类型,这就是为什么比较 stock_level 和 reorder level 值的原因3 - 512 - 20它正在显示,6 -159 - 20它没有显示,因为如果您将 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会将结果转换为数字

希望这可以帮助。

于 2013-03-18T07:05:34.287 回答
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 数据类型。

于 2013-03-18T07:04:49.683 回答