dongni8969 2012-07-11 17:08
浏览 44
已采纳

PHP Mysql从多个表中选择查询

I have 2 tables. The parent table is learn_more and child table is reference_keys Both tables are innodb

reference_keys has two columns:
key_id [index]
key_href


learn_more table
id [primary]
keys_id [foreign key]
page_title    
page_content

What I am trying to do is get multiple links in the learn more table from the reference_keys table. So example, learn_more table id:1, keys_id:1,3,4,8,13,25,..., page_title:Home Page: blah blah, page_content: blah blah......

The problem is that phpmyadmin will not allow me to put more than 1 id in the keys_id of learn_more.

//ERROR
//Warning: #1265 Data truncated for column 'keys_id' at row 1

I'm guessing the relation view is not setup correctly. - How do i fix this?

and on my page it shows the key_id in the echo instead of the value for the id: which is the key_href. so my page show "1" instead of the value for 1 which is a link..

Perhaps my sql query is not correct?

$SQL = "SELECT * FROM learn_more WHERE page_title = '$this_page'";
  • 写回答

3条回答 默认 最新

  • douzhengyi5022 2012-07-11 17:18
    关注

    To build a many-to-many here is what you could do:

    reference_keys has two columns:
    key_id [index]
    key_href
    
    learn_more_to_reference_key
    reference_key_id [FK to reference_keys]
    learn_more_id [FK to learn_more]
    
    learn_more table
    id [primary]
    page_title    
    page_content
    

    Then you have essentially a 1:N on each side of the relationship. Notice that I removed the FK from the learn_more table, too.

    So to grab the relationship you'd query like this:

    SELECT * FROM Learn_More lm 
    INNER JOIN learn_more_to_reference_key lmtrk ON lm.id = lmtrk.learn_more_id 
    INNER JOIN reference_keys rk ON rk.id = lmtrk.reference_key_id
    

    I believe the inner join is correct, i'm double-checking that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?