Searching Multiple Tables (SQL)

柔情痞子 提交于 2021-02-07 04:18:06

问题


I need to be able to have an SQL query that searches my database using a simple search. Here is what my tables look like right now:

Table artists
--------------
id
name

Table albums
-------------
id
artistID
name

Table songs
------------
id
albumID
name

How would I go about doing this? Here are some SQL queries that I have tried, but my issue is that it is returning a lot of data. For instance, if I search for an artist like Snoop Dogg, it is returning a row for each album and song that he has even if they don't contain his name.

SELECT * FROM artist,album,songs WHERE artist.name LIKE '%snoop%' OR albums.name LIKE '%snoop%' OR songs.name LIKE '%snoop%';

Edit:

Here is a sample database;

artists
-----------
1 | Snoop Dogg
2 | Linkin Park

albums
--------
1 | artist=1 | Boom
2 | artist=2 | ThisIsIt

songs
--------
1 | album=1 | First
2 | album=2 | Second Linkin
3 | album=2 | Third
4 | album=1 | Fourth

So I want a search for "snoop" to return just the artist "Snoop Dogg". But then a search like "Linkin" to return the artist and the song.


回答1:


This query will retrieve matching artists, songs and albums. First column gives a clue about origin of the data, second is id and the last is the name.

select 'Artists' OriginatingTable, id, name
  from artists
 where name like '%snoop%'
union all
select 'Albums', id, name
  from albums
 where name like '%snoop%'
union all
select 'Songs', id, name
  from songs
 where name like '%snoop%'



回答2:


The following uses UNION, thus avoids JOINs:

(
    SELECT
        'artist' as `type`,
        `artists`.`id` as `id`,
        `artists`.`name` as `name`
    FROM `artists`
    WHERE
        `artists`.`name` LIKE '%snoop%'
)
UNION
(
    SELECT
        'album' as `type`,
        `albums`.`id` as `id`,
        `albums`.`name` as `name`
    FROM `albums`
    WHERE
        `albums`.`name` LIKE '%snoop%'
)
UNION
(
    SELECT
        'song' as `type`,
        `songs`.`id` as `id`,
        `songs`.`name` as `name`
    FROM `songs`
    WHERE
        `songs`.`name` LIKE '%snoop%'
)



回答3:


You need to join all your data together so it becomes one big data set that will allow you to query against all fields at once. To do this you want to use a left join which is the most optimal for this scenario.

SELECT *
FROM songs
LEFT JOIN albums
ON songs.albumID = albums.id
LEFT JOIN artists
ON albums.artistID = artists.id
WHERE artist.name LIKE '%snoop%'
OR albums.name LIKE '%snoop%'
OR songs.name LIKE '%snoop%';

A union could also be used, but this may not give you the results you want. A left join will give you a row for every song that matches the artist, album, or song. A union will give you a row for each song where where it matches the song name, a row for each album where it matches the album name, and a row for each artist where is matches the artist name.




回答4:


You should select from each table and return and union of the results, but remember that all the select must return the same number and kind of columns.

SELECT 'Artist' as kind, id as artistId, name, null as albumId, null as SongId
FROM artist WHERE artist.name LIKE '%snoop%' 
 union
SELECT 'Album', artistId, name, id, null FROM album WHERE albums.name LIKE '%snoop%'
 union
SELECT 'Song', albums.artistId, songs.name, songs.albumId, songs.id 
 FROM songs inner join albums on songs.albumId = albums.id
 WHERE songs.name LIKE '%snoop%'

This way you avoid getting a combination of all the tables, and you also have all the available info in each row.




回答5:


This query search on artist name, song name and album name

SELECT artist.name,albums.name,songs.name 
FROM songs 
LEFT JOIN album ON album.id = song.albumID
LEFT JOIN artists ON album.artistID = artists.id
WHERE 
artist.name LIKE '%snoop%' OR albums.name LIKE '%snoop%' OR songs.name LIKE '%snoop%'


来源:https://stackoverflow.com/questions/9814522/searching-multiple-tables-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!