duanshan188866 2010-06-11 19:53
浏览 14

高级MySQL搜索帮助

I've been trying to come up with something for a while now to no avail. My MySQL knowledge is rudimentary at best so I could use some guidance on what I should use for the following:

I have 2 tables ('bible' and 'books') that I need to search from. Right now I am just searching 'bible' with the following query:

SELECT * 
  FROM bible 
 WHERE text LIKE '%" . $query . "%' 
 ORDER BY likes DESC 
    LIMIT $start, 10

Now, I need to add another part that searches for some pretty advanced stuff. Here is what I want to do in pseudocode which I am aware doesn't work:

SELECT * 
  FROM bible 
 WHERE books.book+' '+bible.chapter+':'+bible.verse = '$query'

$query would equal something like Genesis 1:2, Genesis coming from books.book, 1 coming from bible.chapter and 2 coming from bible.verse

Any help/guidance on this is much appreciated =)

  • 写回答

4条回答 默认 最新

  • dongqin1075 2010-06-11 19:57
    关注

    You need to parse the query into book, chapter and verse in php first.

    A regular expression should work:

    preg_match("/(.+)([0-9]+):([0-9]+)/",$query,$matches);
    $book    = trim(matches[1]); // use trim to remove extra spaces
    $chapter = matches[2];
    $verse   = matches[3];
    

    Then your sql query becomes:

    SELECT * FROM bible WHERE books.book = '$book' AND bible.chapter= '$chapter' AND bible.verse ='$verse' -- watch out for sql injection here! use prepared statements!

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测