编写一个SQL查询,以按主题和获奖者姓名的顺序查找1970年获奖者的所有详细信息;但是列表最后包含了经济学和化学主题。
YEAR SUBJECT WINNER COUNTRY CATEGORY
---- ------------------------- --------------------------------------------- ------------------------- ------------
1970 Physics Hannes Alfven Sweden Scientist
1970 Physics Louis Neel France Scientist
1970 Chemistry Luis Federico Leloir France Scientist
1970 Physiology Ulf von Euler Sweden Scientist
1970 Physiology Bernard Katz Germany Scientist
1970 Literature Aleksandr Solzhenitsyn Russia Linguist
1970 Economics Paul Samuelson USA Economist
1970 Physiology Julius Axelrod USA Scientist
1971 Physics Dennis Gabor Hungary Scientist
1971 Chemistry Gerhard Herzberg Germany Scientist
1971 Peace Willy Brandt Germany Chancellor
1971 Literature Pablo Neruda Chile Linguist
1971 Economics Simon Kuznets Russia Economist
1978 Peace Anwar al-Sadat Egypt President
1978 Peace Menachem Begin Israel Prime Minister
1987 Chemistry Donald J. Cram USA Scientist
1987 Chemistry Jean-Marie Lehn France Scientist
1987 Physiology Susumu Tonegawa Japan Scientist
1994 Economics Reinhard Selten Germany Economist
1994 Peace Yitzhak Rabin Israel Prime Minister
1987 Physics Johannes Georg Bednorz Germany Scientist
1987 Literature Joseph Brodsky Russia Linguist
1987 Economics Robert Solow USA Economist
1994 Literature Kenzaburo Oe Japan Linguist
answer:
SELECT *
FROM nobel_win
WHERE year=1970
ORDER BY
CASE
WHEN subject IN ('Economics','Chemistry') THEN 1
ELSE 0
END ASC,
subject,
winner;
有人可以在该答案中解释case
和order by
的逻辑吗?
答案 0 :(得分:3)
如果主题是特殊主题之一,则UseSqlServer(connection)
表达式将返回1,否则返回0。由于升序排序,结果集中的0早于1。因此,经济学和化学毕竟是其他学科。
MySQL对此有一个方便的简写,因为布尔值在数字上下文中被视为数字。因此,做同样事情的简短表达式是:
add_filter( 'woocommerce_package_rates', 'custom_hide_shipping', 100 );
function custom_hide_shipping( $rates ) {
$current_time = date_i18n(('H'), current_time('timestamp')); //Uses Timezone Settings > General
$maximum_time = array (
'time' =>'16'); //4PM
if ($current_time >= $maximum['time']){
unset( $rates['flat_rate:X'] ); // change X to your rate id
}
return $rates;
}
答案 1 :(得分:3)
要求说经济学和化学必须是最后的。因此,我们必须使用ORDER BY
来控制结果的顺序。
当subject IN ('Economics','Chemistry')
为true时,CASE
表达式返回1
。否则为0
。由于0
低于1
,并且我们以升序排序,因此该行0
的行将放在第一位。