简体   繁体   中英

help me to write php code for this MySQL sql

CREATE TABLE articles
( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT
, title VARCHAR(99) NOT NULL
, dateposted DATE NOT NULL
, author VARCHAR(99) NOT NULL
);
CREATE TABLE article_rel
( this_article INTEGER NOT NULL REFERENCES articles (id)
, related_article INTEGER NOT NULL REFERENCES articles (id) 
, PRIMARY KEY (this_article,related_article)
);

I am building article site, where i want to show related article. I can write code for articles table , just scratching heads how to do with article_rel table.

SELECT
  a.*
FROM
  articles a
INNER JOIN
  article_rel ar
    ON
  ar.this_article = SOME_ARTICLE_ID_HERE
    AND
  ar.related_article = a.id

Brain is fried, but I think that will do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM