douyi2664
douyi2664
2013-07-10 09:00

搜索多个表格中的前三个字母

已采纳

movie_table

id  movie_name      genre   rating
1   Green lantern   action  7.5
2   Seven           horror  8.5
4   django          comedy  6.0

tv_serial

id  serial_name     genre   rating
1   game of throne  action  9.5
2   walking dead    horror  8.5
4   breaking bad    comedy  6.0

Now if I put any name starting with G in the search bar the list must show green lantern and game of throne. Just need the sql query to extract these data thank you.

Edit

To sound more generalized and to be helpful for others, this question is rephrased as: How to implement Trie in php where data is obtained from a mysql table?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dsafq2131321 dsafq2131321 8年前

    Try following query :

    SELECT movie_name as Name
    FROM movie_table
    WHERE movie_name like 'G%'
    union all
    SELECT serial_name as Name
    FROM tv_serial
    WHERE serial_name like 'G%'
    

    SQLFIDDLE

    点赞 评论 复制链接分享