简体   繁体   English

MYSQL(WHERE-IF-SELECT / IN语句)

[英]MYSQL (WHERE - IF - SELECT/IN statement)

I'm trying to set my code up where I have a Control Panel for my Sales Reps. From my Control Panel, I am able to control which customers a Rep calls based on the item the customer bought. 我试图在我的销售代表控制面板上设置代码,然后从控制面板中根据客户购买的商品来控制销售代表致电给哪些客户。

ie If I assign "Cars" in my Control Panel for Sales Rep 01, he would only call customers that bought toys in the "Cars" category. 例如,如果我在销售代表01的控制面板中分配了“汽车”,他将仅致电购买“汽车”类别中的玩具的客户。 And all different toys in my "Cars" category are stored in a separate file named "Vehicles". 我的“汽车”类别中的所有不同玩具都存储在名为“车辆”的单独文件中。

My files are as follows: 我的文件如下:

customer (File)
+--------+-----------+--------+
|  name  | phone     | toy    |
+--------+-----------+--------+
|  Gail  | 777-1234  | Truck  |
|  June  | 777-1235  | Doll   |
|  Mary  | 777-1236  | Racer  |
|  Bill  | 777-1237  | Ball   |
|  Jon   | 777-1238  | Jeep   |
+--------+-----------+--------+



control_panel (File)
+----------+--------+
| user     | desc   | 
+----------+--------+
| sales_01 | Cars   |
+----------+--------|


vehicles (File)
+---------+
+  item   | 
+---------+
|  Truck  |
|  Racer  |
|  Jeep   |
+---------+

In trying to test this code out, I have this portion of my code working. 在尝试测试此代码时,我将这部分代码工作了。

select 
    c.name , c.phone
FROM 
    customer c
WHERE
    c.toy IN (
            SELECT 
                v.item 
            FROM 
                vehicles v
            )

Now I'm trying to condition my WHERE statement so that only if I choose "Cars" in my Control Panel screen for User "sales_01", then customers who bought Cars will only show on the Call Screen for User "sales_01". 现在,我试图对WHERE语句进行条件处理,以便仅当我在“控制面板”屏幕中为用户“ sales_01”选择“汽车”时,购买汽车的客户才会在用户“ sales_01”的呼叫屏幕上显示。

This is one example of some of the code I've been testing but cant get to work correctly. 这是我正在测试但无法正常工作的一些代码的示例。

SELECT   
    c.name , c.phone
FROM
    customer c , control_panel p
WHERE
    (IF p.desc = "Cars" 
        THEN (c.toy 
            IN (SELECT 
                    v.item 
                FROM
                    vehicles v)
             )
     END)

Any help is appreciated. 任何帮助表示赞赏。 Thx. 谢谢。

To start with, I suggest you refactor your schema by adding an field category to your vehicles table. 首先,建议您通过将字段category添加到vehiclescategory来重构架构。 This will allow a proper relationship between your control_panel and customer . 这将使您的control_panelcustomer之间具有适当的关系。 In that case you could just do SELECT c.name, c.phone FROM customer c LEFT OUTER JOIN vehicles v ON c.toy = v.items WHERE v.category = 'Cars' . 在这种情况下,您可以只SELECT c.name, c.phone FROM customer c LEFT OUTER JOIN vehicles v ON c.toy = v.items WHERE v.category = 'Cars' I would have loved to add other suggestions but I feel you want something that could work right now. 我很想补充其他建议,但我觉得您想要可以立即使用的功能。 Plus, I do not have much time to spare presently. 另外,我现在没有太多时间可利用。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM