Help us understand the problem. What is going on with this article?

PHPからSQLを記述してデータベースにアクセスして情報を取得(2テーブル連結ver.)

More than 1 year has passed since last update.

PHPからSQLを書いて、データベースにアクセスして、テーブル内のレコードを取得する際、2つのテーブルを連結させる必要性があったので、下記のように対応。

メモの意味で残しておく。

$_SELECT    = "n.cd AS n_cd, n.title AS n_title, n.news_category_cd AS n_news_category, n.date AS n_date, n.comment AS n_comment, n.img1 AS n_img1, n.link AS n_link, n.target AS n_target, ";
$_SELECT   .= "n_c.cd AS n_c_cd, n_c.name AS n_c_name";
$sql        =

    "SELECT 
        {$_SELECT}
    FROM 
        news AS n 
    INNER JOIN news_category AS n_c ON n.news_category_cd = n_c.cd 
    WHERE 
        n.disp_flg = 1 
        AND n_c.disp_flg = 1 
    ORDER BY 
        n.sort, 
        n_c.sort
    LIMIT 3";

{$_SELECT}のように取得するカラム部分を変数にしておくといいかもしれませんね。

↓のサイトでSQLを読みやすいようにフォーマットしてくれるので、活用するようにするといい。
https://araishi.com/sql-formatter/

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away