Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@ryuji62

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/

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  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
ryuji62
プログラマです。 以下経験↓ ・HTML、CSSを用いたコーディング ・JS(Jquery)、CSS3を用いた動き ・PHP、SQLを用いたデータベースの構築とオリジナルCMSの作成 ・JAVAを用いたAndroidアプリ制作(実践はないが、特に勉強中) ・gulpを用いてejsを用いてのコーディング環境構築 ・Laravel

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?