doujuan9698 2013-09-06 16:57
浏览 51
已采纳

如何在sql查询中搜索外键表的列名?

I going to create a search function in my project but found that some problem in selecting the column record from another foreign key's table is make me frustrated .

This is a normal sample output before searching: enter image description here

This is a issued table after searching: enter image description here

mysql query of my table named "member_booking" enter image description here

If I'm search the title which is based on event table,it will show me a string of title record where no restricted by Foreign key.

mysql query may look like this:

select *
from member_booking,event,member
where member_booking.E_ID=event.E_ID and member_booking.MEM_ID=member.MEM_ID and  member_booking.REF_CODES like '%$keyword%' OR event.E_TITLE like '%$keyword%' OR member.MEM_USERNAME like '%$keyword%' OR member.MEM_EMAIL like '%$keyword%' 

Thanks.

  • 写回答

1条回答 默认 最新

  • du5910 2013-09-06 17:16
    关注

    Be careful of SQL injection attack if you inject $keyword into those "like" operators. Use "inner join" or left/right join to join the tables.

    for example.

    select 
    -- your comma separated list of fields here, for example
    -- e.event_title , e.amount , e.event_date , e.MEM_ID, 
    from 
    member_booking mb inner join event e on mb.E_ID = e.E_ID 
    inner join member m on mb.MEM_ID = m.MEM_ID
    where  mb.REF_CODES like '%$keyword%' 
    OR e.E_TITLE like '%$keyword%' 
    OR m.MEM_USERNAME like '%$keyword%' 
    OR m.MEM_EMAIL like '%$keyword%' ** 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程