我有两个桌子.一个拥有製造商資訊,並包括他们可以销售的區域.另一个有他们的产品出售.我们必须根据地區来限製产品的可见性.就像Netflix的系統中的视频只能在任何地方(1),在加拿大(2),在美国(3)都可以观看。
我正在尝試进行查詢,以根据製造商表中的設置告诉我可以在哪裏查看产品。
例如,在製造商表中,有两个欄位稱為Exposure_new和Exposure_used,每个欄位的值分別為1,2或3,以限製可以观看其新视频或使用過的视频的位置。
添加视频時,不会為它们分配"曝光"值,這是根据当前製造商的Exposure_new或Exposure_used值在將它们添加到我们的索引中時即時进行的。
What I am trying to get is the item details and the computed value for where it can be seen based on whether it is new or used and the rule/value assigned to the manufacturer for all their new or used products. 我需要基於每个产品的一位數字,以便有條件地將其顯示在列表中。
以下操作無效,但是您將了解我正在尝試執行的操作.我已经使用CASE語句和以下wRONG IF / ELSEIF語句进行了尝試。
任何帮助除錯此錯誤並將其指向正確方向的帮助,將不胜感激。
SELECT
t2.company_name,
t2.expose_new, // 1,2 or 3
t2.expose_used, // 1,2 or 3
t1.title,
t1.seller,
t1.status, //can be new or used
(SELECT
IF(status ='New',
(select expose_new from manufacturers where id = t1.seller),1
)
ELSEIF(t1.status ='Used',
(select expose_used from manufacturers where id = t1.seller),1
)
END IF
) as 'expose'
FROM `products` t1
join manufacturers t2 on t2.id = t1.seller
where t1.seller = 4238
這是一个CASE版本,實際上似乎在執行,但無論什麼情况發生(在本例中為1),总是会产生第一个值.我不確定我是否可以在每个wHEN語句中使用AND来添加另一个測試,但是它不会给出錯誤,而只会给出錯誤的結果。
SELECT
t2.company_name,
t2.expose_new,
t2.expose_used,
t1.title,
t1.status,
CASE status
when 'New' and t2.expose_new = 1 then 1
when 'New' and t2.expose_new = 2 then 2
when 'New' and t2.expose_new = 3 then 3
when 'Used' and t2.expose_used = 1 then 1
when 'Used' and t2.expose_used = 2 then 2
when 'Used' and t2.expose_used = 3 then 3
END as expose
FROM `products` t1
join manufacturers t2 on t2.id = t1.seller
where t1.seller = 4238
- 6月前1 #
- 6月前2 #
Syntax:
CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result] END
Alternative: 案例[條件]然後結果 [何時[條件] 然後結果...]
mysql> SELECT CASE WHEN 2>3 THEN 'this is true' ELSE 'this is false' END; +-------------------------------------------------------------+ | CASE WHEN 2>3 THEN 'this is true' ELSE 'this is false' END | +-------------------------------------------------------------+ | this is false | +-------------------------------------------------------------+
I am use:
SELECT act.*, CASE WHEN (lises.session_date IS NOT NULL AND ses.session_date IS NULL) THEN lises.location_id WHEN (lises.session_date IS NULL AND ses.session_date IS NOT NULL) THEN ses.location_id WHEN (lises.session_date IS NOT NULL AND ses.session_date IS NOT NULL AND lises.session_date>ses.session_date) THEN ses.location_id WHEN (lises.session_date IS NOT NULL AND ses.session_date IS NOT NULL AND lises.session_date<ses.session_date) THEN lises.location_id END AS location_id FROM activity AS act LEFT JOIN li_sessions AS lises ON lises.activity_id = act.id AND lises.session_date >= now() LEFT JOIN session AS ses ON ses.activity_id = act.id AND ses.session_date >= now() WHERE act.id
- 6月前3 #
執行此操作的另一種方法是使用巢狀的IF語句.假設您有公司表,並且您想計算其中的記錄數.一个示例查詢就是這樣
SELECT IF( count(*) > 15, 'good', IF( count(*) > 10, 'average', 'poor' ) ) as data_count FROM companies
当第一个IF條件失败時,第二个IF條件起作用.因此,IF語句的示例語法為IF(CONDITION,THEN,ELSE)。 希望對別人有帮助。
相似問題
- sql:MySQL Select查詢-仅获取值的前10个字元mysqlsqlselect2021-01-09 00:23
- select:MySQL-SQL_BIG_SELECTSmysqlselect2021-01-08 07:57
尝試此查詢-