doupai8095 2015-06-07 23:43
浏览 135
已采纳

将INT(11)与FLOOR()进行比较会导致MySQL

I have a table called quotes and I wanted to create a MySQL query that returns the data from a random quote. I searched over the internet and found how to create a random number from 1 to the number of quotes in the table:

SELECT FLOOR(RAND() * (SELECT ((SELECT COUNT(*) FROM DAILYS) - 1))) + 1 AS RANDNUM;

I have tested this and it works, it returns a random number every time and in the range that I need.

The problem is that I need to get information from two tables, QUOTES and AUTHORS. This has worked before:

SELECT QUOTES.QUOTE, AUTHORS.NAME, AUTHORS.LINK
FROM QUOTES JOIN AUTHORS ON QUOTES.AUTHOR_ID = AUTHORS.ID
WHERE QUOTES.ID =
(SELECT QUOTE_ID FROM DAILYS WHERE DAY = CURDATE());

Now I wanted to do the exact same thing, but insted of comparing the quote ID with today's ID, I will compare it to the random number. The type from the ID column is "int(11)" according to phpMyAdmin, so I don't know what might be wrong with it. Thank you for your time!

  • 写回答

1条回答 默认 最新

  • doudui6756 2015-06-08 02:53
    关注

    Your first query yields an integer. You can't compare that to a date (assuming that's what you're trying to do).

    I combined your queries using the answer to this question:

    SELECT QUOTES.QUOTE, AUTHORS.NAME, AUTHORS.LINK
    FROM QUOTES
    JOIN AUTHORS ON QUOTES.AUTHOR_ID = AUTHORS.ID
    WHERE QUOTES.ID =
    (SELECT QUOTE_ID FROM DAYLYS ORDER BY DAY LIMIT
    (SELECT FLOOR(RAND() * (SELECT ((SELECT COUNT(*) FROM DAILYS) - 1)))),1);
    

    I haven't tested this, but I'm fairly confident it should work (if I understand your database design correctly).

    Edit: According to another answer on the page I referenced, "the offset is zero-indexed", so I removed "+ 1".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MySQL创建时出现1064以下情况怎么办?
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重