MySQLの初心者です。table_aとtable_bがあり、table_aのidとtable_bのt_id は共通している場合に、
同じidを持っている、table_aとtable_bのFieldの表を出したいのですが、下記のようにしてもエラーが出てしまいます。
どう書いたら[最終形]のように出来るでしょうか
現在の環境はMac OS10.14.6 のターミナルで、mysql Ver 14.14 Distrib 5.7.37です。
[現在の状態]
MySQL
1 2table_a 3mysql> show fields from table_a; 4+-----------+----------+------+-----+---------+-------+ 5| Field | Type | Null | Key | Default | Extra | 6+-----------+----------+------+-----+---------+-------+ 7| id | char(40) | NO | | NULL | | 8| a_num | int(11) | YES | | NULL | | 9| a_memo | text | YES | | NULL | | 10+-----------+----------+------+-----+---------+-------+ 113 rows in set (0.02 sec) 12 13table_b 14mysql> show fields from table_b; 15+-----------+----------+------+-----+---------+-------+ 16| Field | Type | Null | Key | Default | Extra | 17+-----------+----------+------+-----+---------+-------+ 18| t_id | char(40) | NO | | NULL | | 19| b_num | int(11) | YES | | NULL | | 20| b_memo | text | YES | | NULL | | 21+-----------+----------+------+-----+---------+-------+ 223 rows in set (0.02 sec) 23 24 25> SELECT a_num,a_memo,b_num,b_memo from test.table_a,test.table_b where id='12345' and t_id='12345'; 26 27ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a_num,a_memo,b_num,b_memo from test.table_a test.table_b where t_' at line 1
[最終形]こんな形で出したいと思っています
MySQL
1+-----------+----------+---------+------------+ 2| a_num | a_memo | b_num | b_memo | 3+-----------+----------+---------+------------+ 4| 1111 | あいうえお | 11111 | アイウエオ | 5| 2222 | かきくけこ | 22222 | カキクケコ | 6| 3333 | さしすせそ | 22222 | サシスセソ | 7+-----------+----------+---------+-------------+
追記
下記のような感じでやりたいことが出来ました。ありがとうございました。
mysql> select a_num, a_memo
FROM test.table_a INNER JOIN test.table_b where id = t_id and a_memo like '%あ%';
mysql> select a_num, a_memo
FROM test.table_a,test.table_b where id = t_id and a_memo like '%あ%';
回答3件
あなたの回答
tips
プレビュー