MySQL加入给出不一致的结果

时间:2017-08-11 20:48:00

标签: mysql

我正在尝试计划我的饭菜:

select * from 
    (select floor(rand() * 3) + 1 as rand_id, days1.* from (
          select 'Monday' as dy from dual
    union select 'Tuesday' from dual
    union select 'Wednesday' from dual
    union select 'Thursday' from dual
    union select 'Friday' from dual
    ) days1) days
left join 
    (select id as rand_id, meals1.* from  (
          select 1 as id, 'Pizza' as dinner  from dual
    union select 2, 'Hotdogs' from dual
    union select 3, 'Spaghetti' from dual)meals1) meals
on days.rand_id = meals.rand_id;

当我在SQL Fiddle it works fine上运行此查询时,但是当我使用我的本地mysql实例尝试它时,我得到了完全的乱码结果:随机连接的随机行数:

+---------+-----------+---------+------+---------+
| rand_id | dy        | rand_id | id   | dinner  |
+---------+-----------+---------+------+---------+
|       1 | Wednesday |       2 |    2 | Hotdogs |
|       1 | Monday    |    NULL | NULL | NULL    |
|       3 | Tuesday   |    NULL | NULL | NULL    |
|       3 | Friday    |    NULL | NULL | NULL    |
+---------+-----------+---------+------+---------+

+---------+-----------+---------+------+-----------+
| rand_id | dy        | rand_id | id   | dinner    |
+---------+-----------+---------+------+-----------+
|       3 | Wednesday |       1 |    1 | Pizza     |
|       1 | Wednesday |       3 |    3 | Spaghetti |
|       2 | Thursday  |    NULL | NULL | NULL      |
|       3 | Friday    |    NULL | NULL | NULL      |
+---------+-----------+---------+------+-----------+

我期望看到的是5行,每行在rand_id中的随机数介于1和3之间,days.rand_id和meals.rand_id都是相同的。我希望每次运行查询时,我会在一周中的每一天获得一行随机选择的一餐。在我的本地mysql(但不是sqlfiddle的mysql)中可能出现什么问题,它给了我这个输出?

(注意:最初的目标是随机链接真实的客户记录和假的客户数据以生成测试数据,但我已经简化了这个例子)

0 个答案:

没有答案