PHP MySQL连接两个表

PHP MySQL连接两个表,php,mysql,Php,Mysql,我有两张桌子,歌曲和历史。“歌曲”表如下所示: ID | title | artist | duration 1 | some title | some artist | 83592 ID | title | artist | duration | date_played 5 | some title | some artist | 83592 | 2012-08-08 11:22:00 历史记录表如下所示: ID | title

我有两张桌子,歌曲和历史。“歌曲”表如下所示:

ID | title      | artist      | duration
1  | some title | some artist | 83592
ID | title      |   artist    | duration | date_played
5  | some title | some artist | 83592    | 2012-08-08 11:22:00
历史记录表如下所示:

ID | title      | artist      | duration
1  | some title | some artist | 83592
ID | title      |   artist    | duration | date_played
5  | some title | some artist | 83592    | 2012-08-08 11:22:00
如果历史记录表中最近条目的标题和艺术家匹配,我将如何从歌曲表中回显ID

我尝试了
SELECT*FROM history JOIN songs ON title=songs.title AND artist=songs.artist ORDER BY date_play DESC LIMIT 0,1
,但没有成功。有什么想法吗?

检查这个

select songs1.id,history1.title,history1.artist 
from songs as songs1,history as history1
order by date_diplayed desc
我认为这个查询可以解决您的问题

检查这个

select songs1.id,history1.title,history1.artist 
from songs as songs1,history as history1
order by date_diplayed desc
SELECT s.ID
FROM songs s
INNER JOIN (SELECT * FROM history h ORDER BY date_played DESC LIMIT 1) lastHistory
ON lastHistory.title = s.title AND lastHistory.artist = s.artist
我认为这个问题能解决你的问题

SELECT s.ID
FROM songs s
INNER JOIN (SELECT * FROM history h ORDER BY date_played DESC LIMIT 1) lastHistory
ON lastHistory.title = s.title AND lastHistory.artist = s.artist
()

()

上面的查询创建并内联最近播放的歌曲的视图,称为hist_视图(使用LIMIT and ORDER BY DESC)。然后,它与歌曲表结合,根据艺术家和标题查找匹配的歌曲

我建议您在历史记录表中添加类似song_id的内容作为外键

SELECT * FROM history A INNER JOIN songs B 
ON A.title=B.title AND A.artist=B.artist 
ORDER BY A.date_played DESC
上面的查询创建并内联最近播放的歌曲的视图,称为hist_视图(使用LIMIT and ORDER BY DESC)。然后,它与歌曲表结合,根据艺术家和标题查找匹配的歌曲

我建议您在历史记录表中添加类似song_id的内容作为外键

SELECT * FROM history A INNER JOIN songs B 
ON A.title=B.title AND A.artist=B.artist 
ORDER BY A.date_played DESC
我的建议是在历史表中,您可以使用歌曲的歌曲id表,而不是艺术家和标题

表:歌曲

ID | title      | artist      | duration
1  | some title | some artist | 83592
表:历史

ID | songid  | date_played
5  | 1       | 2012-08-08 11:22:00
这样您就可以在模式中进行一些优化

然后您可以尝试此查询

SELECT * FROM history A INNER JOIN songs B 
ON A.songid=B.ID ORDER BY A.date_played DESC
我的建议是在历史表中,您可以使用歌曲的歌曲id表,而不是艺术家和标题

表:歌曲

ID | title      | artist      | duration
1  | some title | some artist | 83592
表:历史

ID | songid  | date_played
5  | 1       | 2012-08-08 11:22:00
这样您就可以在模式中进行一些优化

然后您可以尝试此查询

SELECT * FROM history A INNER JOIN songs B 
ON A.songid=B.ID ORDER BY A.date_played DESC
你可以用

SELECT   songs.id
FROM     songs,
         history
WHERE    songs.title = history.title
AND      songs.artist = history.artist
ORDER BY history.date_played DESC

但是如果你按照Vinay的建议组织你的桌子会更好。

你可以使用

SELECT   songs.id
FROM     songs,
         history
WHERE    songs.title = history.title
AND      songs.artist = history.artist
ORDER BY history.date_played DESC


但是如果你按照Vinay的建议组织你的表格会更好。

不,这只会做两个表格的完全笛卡尔积不,这只会做两个表格的完全笛卡尔积