douya2006
2011-07-27 13:25
浏览 36
已采纳

MySQL从不同的表中检索2个值

I didn't know how to explain it in so few words as to fit it in the title. However it's not really complicated. I have 2 tables, the first called "Lines" has these 2 columns:

Text  PID

and the second called Poems these 2:

PID Title

I need to SELECT rows from the first table when they are equal to $something and then look up on the table Poems for the matching row (the one that has the same PID) and display both the Text from the first table and the Title from the second.

How can this be done? Thanks

图片转代码服务由CSDN问答提供 功能建议

我不知道如何用很少的单词解释它以使其适合标题。 然而,它并不复杂。 我有2个表,第一个名为“Lines”的表有这两列:

  Text PID 
   
 
 

和 第二个叫诗歌这两个:

  PID Title 
   
 
 

我需要从第一个表中选择行 它们等于$ something,然后在桌子上查找匹配行(具有相同PID的那个)的诗歌,并显示第一个表格中的文本和第二个表格中的标题。 \ n

如何做到这一点? 谢谢

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongmeng1868 2011-07-27 13:28
    已采纳
    SELECT Lines.*, Poems.* FROM Lines 
    INNER JOIN Poems ON(Poems.PID = Lines.PID)
    WHERE Lines.Text = 'SOME VALUE'
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtnd30892 2011-07-27 13:28
    SELECT l.Text, p.Title
        FROM lines l
            INNER JOIN poems p
                ON l.PID = p.PID
        WHERE l.text = 'something'
    
    评论
    解决 无用
    打赏 举报
  • dongshenchi5364 2011-07-27 13:29

    You are looking for an Inner Join SQL query:

        SELECT column_name(s) FROM table_name1 INNER JOIN 
    table_name2 ON table_name1.column_name=table_name2.column_name
    WHERE table_namex.column_n = "xyz";
    

    Which implies that in you case you will use:

    SELECT Lines.*, Poems.*
        FROM Lines l INNER JOIN Poems p
                ON l.PID = p.PID
        WHERE l.text = "xyz"
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题