早上好,我一直在尝试从三个表中进行简单搜索,这是多么的可能。 我有这样的桌子
“歌曲”表中的列如song_title,song_author。 视频表中的列如video_title,videos_author。 虽然posts表还具有post_title和post_author。
但是我想从这些列的搜索输入框中搜索关键字 “歌曲标题,歌曲作者,视频标题,视频,作者,帖子标题”
到目前为止,我尝试执行的操作似乎完全没有用:
$query = "(SELECT song_title, song_author, 'song' as type FROM songs WHERE song_title LIKE '%" . $keyword . "%' OR song_author LIKE '%" . $keyword ."%')
UNION
(SELECT video_title, video_author, 'video' as type FROM videos WHERE video_title LIKE '%" . $keyword . "%' OR video_author LIKE '%" . $keyword ."%')
UNION
(SELECT post_title,'post' as type FROM posts WHERE post_title LIKE '%"')";
mysqli_query($connection,$query);
谢谢您。
答案 0 :(得分:1)
您可能会找到的SQL查询是:
SELECT * FROM (
SELECT song_title AS title, song_author AS author, 'song' as type FROM songs
UNION
SELECT video_title AS title, video_author AS author, 'video' as type FROM videos
UNION
SELECT post_title AS title, NULL AS author, 'post' as type FROM posts) a
WHERE title LIKE '%'.$keyword.'%' OR author LIKE '%'.$keyword.'%'
另外,请考虑另外两个重要说明: