Chrisj Posted June 17, 2016 Share Posted June 17, 2016 In the PHP/SQL script I'm using the search query is only done on one table at a time, currently. I'd like help creating combined search results "All" from the videos table and images table when similar "tags" are searched. Can a Union be used to display just specific, similar columns from each table?Currently, the separate 'search results' are displayed the same way. The 'image search results' display an image, title, description, in a results column on the page. The 'video search results' display a thumbnail image, title, description, in a results column on the page. I've attached a shot of the 'images' table and a shot of the 'videos' table. Any guidance will be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/301355-trying-to-create-a-union-between-two-tables/ Share on other sites More sharing options...
kicken Posted June 18, 2016 Share Posted June 18, 2016 So long as the end results have the same number and types of columns you can do a UNION of two separate queries. So you're query would look something like: SELECT video_id as id, title, description FROM videos UNION ALL SELECT image_id as id, title, description FROM images That would give you a single result set with three columns: id, title, and description. You'll also likely want some way to know whether a given row is an image or a video. To do that you can add a static column to each query that indicates the row type. SELECT 'video' as rowType, video_id as id, title, description FROM videos UNION ALL SELECT 'image' as rowType, image_id as id, title, description FROM images Quote Link to comment https://forums.phpfreaks.com/topic/301355-trying-to-create-a-union-between-two-tables/#findComment-1533784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.