Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993

Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php:4991) in /www/htdocs/v030397/mysql-qotd/wp-includes/feed-rss2.php on line 8
MySQL Question of the Day » plogi http://mysql-qotd.casperia.net mysql 5.0/5.1 questions for learning purposes Fri, 06 Aug 2010 16:42:05 +0000 http://wordpress.org/?v=abc en hourly 1 Question 85: What is the result of the query? http://mysql-qotd.casperia.net/archives/562 http://mysql-qotd.casperia.net/archives/562#comments Fri, 06 Aug 2010 16:41:45 +0000 plogi http://mysql-qotd.casperia.net/?p=562
select n,n and 3,n & 3, n mod 4, n and false,n and true
from (select 0 n union select 1 union select 2 union
      select 3 union select 4 union select 5 union
      select 6 union select 7 union select null) v1;


 
Hints:
The subquery v1 returns column n with the rows 0,1,2,3,4,5,6,7,null.
“&” is the binary and-operation. “MOD” is the modulo (remainder) calculation.
Mysql treats 0 as false and anything else as true.
The constants TRUE and FALSE have the values 1 and 0.

]]>
http://mysql-qotd.casperia.net/archives/562/feed 1
More uncommon SQL http://mysql-qotd.casperia.net/archives/560 http://mysql-qotd.casperia.net/archives/560#comments Sun, 25 Jul 2010 19:55:11 +0000 plogi http://mysql-qotd.casperia.net/?p=560 Hi again!
The query below uses quite a bit of strange looking SQL syntax:
select all
  count(1) minus
union all
  select 1 only
order by 1 limit 1  ;

+-------+
| minus |
+-------+
|     1 |
+-------+
1 row in set (0.00 sec)


 
The “ALL” in the “SELECT ALL” is actually the default – so you hardly ever see it.
“COUNT(1)” is mostly used with Oracle, where it may have a performance benefit over “COUNT(*)”. The result however is the same than “COUNT(*)”.
The “minus” and “only” are column aliases.
Last but not least, the “ORDER BY 1″ means, that we order by the first column.

Below the same slightly more readable:

select
  count(*) as cnt
union all
  select 1
order by cnt limit 1  ;

]]>
http://mysql-qotd.casperia.net/archives/560/feed 0
Uncommon SQL 3 http://mysql-qotd.casperia.net/archives/556 http://mysql-qotd.casperia.net/archives/556#comments Wed, 21 Jul 2010 16:57:33 +0000 plogi http://mysql-qotd.casperia.net/?p=556 select * from vending_price where (price,product_id) = any (select max(price),product_id [...]]]> Hello!
The following query does the same than yesterday’s, featuring another syntax which isn’t used that often.
MariaDB [tt]> select * from vending_price
where (price,product_id) = any (select max(price),product_id
                                            from vending_price
                                            group by product_id);

The “ANY/ALL” syntax can be very intuitive for some queries… Unluckliy not for this example %)

]]>
http://mysql-qotd.casperia.net/archives/556/feed 0
Uncommon SQL 2 http://mysql-qotd.casperia.net/archives/553 http://mysql-qotd.casperia.net/archives/553#comments Tue, 20 Jul 2010 14:19:18 +0000 plogi http://mysql-qotd.casperia.net/?p=553 select * from vending_price where (price,product_id) in (select max(price),product_id [...]]]> Hi again!
Continuing the example from yesterday, the following query shows the the real power of that syntax:
MariaDB [tt]> select * from vending_price where (price,product_id) in
                      (select max(price),product_id
                       from vending_price group by product_id);

]]>
http://mysql-qotd.casperia.net/archives/553/feed 0
Uncommon SQL http://mysql-qotd.casperia.net/archives/549 http://mysql-qotd.casperia.net/archives/549#comments Mon, 19 Jul 2010 19:03:49 +0000 plogi http://mysql-qotd.casperia.net/?p=549 Hi!
There are some useful SQL structures, which are not so commonly used. For example I like the following syntax:
select * from vending_price where (product_id,valid_from) = (0,'2010-01-09');


It is basically the same than the following, but more compact and easier to read.

select * from vending_price where product_id = 0 and valid_from  =  '2010-01-09';

]]>
http://mysql-qotd.casperia.net/archives/549/feed 0
No questions for a while http://mysql-qotd.casperia.net/archives/543 http://mysql-qotd.casperia.net/archives/543#comments Wed, 14 Jul 2010 18:42:32 +0000 plogi http://mysql-qotd.casperia.net/?p=543 Hi!
I won’t be able to post questions for a while…
Lots of things to do and everybody else is on holiday.
And then soon I’ll be on holiday :)

CU!

]]>
http://mysql-qotd.casperia.net/archives/543/feed 0
Question 84: Which of the following are equivalent? http://mysql-qotd.casperia.net/archives/541 http://mysql-qotd.casperia.net/archives/541#comments Tue, 13 Jul 2010 17:20:39 +0000 plogi http://mysql-qotd.casperia.net/?p=541 10 and b < 20 4) where a = 1 or a = 2 or a = 3 5) where b between 10 and 20 6) where b >= 10 and b < = 20 7) where b >= [...]]]> 1) where a in (1,2,3)
2) where a = 1 and a = 2 and a = 3
3) where b > 10 and b < 20
4) where a = 1 or a = 2 or a = 3
5) where b between 10 and 20
6) where b >= 10 and b < = 20
7) where b >= 10 and b < 20
 
Which of the following are equivalent?
(Find all correct answers)
 
a) 3 and 5
b) 5 and 6
c) 1 and 4
d) 5 and 7
e) 1 and 2
 
[Just for fun]

]]>
http://mysql-qotd.casperia.net/archives/541/feed 1
Question 83: How to get the correct price? http://mysql-qotd.casperia.net/archives/537 http://mysql-qotd.casperia.net/archives/537#comments Mon, 12 Jul 2010 18:03:48 +0000 plogi http://mysql-qotd.casperia.net/?p=537 Extending on the scenario of Q80:
We want to read the correct historical price and sum up per product/day.
The price table is below…

drop table if exists vending_price ;
create table vending_price
  (product_id int,valid_from date,valid_to date,price decimal (8,2),
  primary key (product_id,valid_from,valid_to)) engine = innodb;
insert into vending_price
  select v1.product_id,v2.d,adddate(v2.d,interval 3 day),rand() * 15 from
    (select distinct product_id from vending_log) v1,
    (select '2010-01-01' as d union select '2010-01-05' union
     select '2010-01-09' union select '2010-01-13' union select '2010-01-17' ) v2;


 
[Just for fun]

]]>
http://mysql-qotd.casperia.net/archives/537/feed 1
Question 82: More solutions for Q80? http://mysql-qotd.casperia.net/archives/535 http://mysql-qotd.casperia.net/archives/535#comments Sun, 11 Jul 2010 11:43:14 +0000 plogi http://mysql-qotd.casperia.net/?p=535 Hello again!
Still about question 80… We have now 3 solutions to read values from the previous row: Subquery, user variables and with a helper-table + join. There is another feature (at least) which could be used, maybe a more procedural way? (hint, hint)
 
[Just for fun]

]]>
http://mysql-qotd.casperia.net/archives/535/feed 1
Question 81: More solutions for Q80? http://mysql-qotd.casperia.net/archives/533 http://mysql-qotd.casperia.net/archives/533#comments Sat, 10 Jul 2010 17:52:32 +0000 plogi http://mysql-qotd.casperia.net/?p=533 Hi!
It is really important to find as many different solutions as possible for a problem. The reason is, that sometimes a certain solution won’t work depending on the situation. So you need to have alternatives.
For question 80 I offered two possibilities, one with user-variables and one with a subquery. Are there more?

[Just for fun]

]]>
http://mysql-qotd.casperia.net/archives/533/feed 1