質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
phpMyAdmin

phpMyAdminはオープンソースで、PHPで書かれたウェブベースのMySQL管理ツールのことです。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Q&A

解決済

1回答

2104閲覧

SQL文のエラー、COUNT、副問い合わせ

Chandler_Bing

総合スコア673

phpMyAdmin

phpMyAdminはオープンソースで、PHPで書かれたウェブベースのMySQL管理ツールのことです。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

1グッド

0クリップ

投稿2018/12/15 07:07

SQL

1SELECT COUNT(*) FROM (history.productid AS productid, history.tableNumber AS tableNumber, history.quantity AS quantity, history.time AS time, 2 sushi.name AS name, sushi.price AS price, sushi.img AS img, sushi.intro AS intro 3 FROM history INNER JOIN sushi ON (history.productid = sushi.id) 4 WHERE history.tableNumber = ? 5 UNION ALL 6 SELECT history.productid AS productid, history.tableNumber AS tableNumber, history.quantity AS quantity, history.time AS time, 7 dessert.name AS name, dessert.price AS price, dessert.img AS img, dessert.intro AS intro 8 FROM history INNER JOIN dessert ON (history.productid = dessert.id) 9 WHERE history.tableNumber = ?)

でエラーが出ます。全件数が欲しいのですが、修正箇所を教えてください。
使用テーブルは以下の3つになります。

イメージ説明
イメージ説明
イメージ説明

bochan2👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

aikon_marimo

2018/12/15 07:13

どのようなエラーがでるのか記載願います。
Chandler_Bing

2018/12/15 07:35

有難うございます。 SQL query: Documentation SELECT COUNT(*) FROM (SELECT history.productid AS productid, history.tableNumber AS tableNumber, history.quantity AS quantity, history.time AS time, sushi.name AS name, sushi.price AS price, sushi.img AS img, sushi.intro AS intro FROM history INNER JOIN sushi ON (history.productid = sushi.id) WHERE history.tableNumber = 6 UNION ALL SELECT history.productid AS productid, history.tableNumber AS tableNumber, history.quantity AS quantity, history.time AS time, dessert.name AS name, dessert.price AS price, dessert.img AS img, dessert.intro AS intro FROM history INNER JOIN dessert ON (history.productid = dessert.id) WHERE history.tableNumber = 6) LIMIT 0, 30 MySQL said: Documentation #1046 - No database selected この様なエラーが出ています。 ?の部分には6という数字を仮に入れております。 回答有難うございます。
aikon_marimo

2018/12/15 07:40

エラー文言にあるように、データベースが選択されていないエラーになります。 どのようにSQL実行しているか分かりませんが、 ①ターミナルでSQL実行しているなら、use データベース名(Sushi?)でデータベースを選択後、上記SQLを実行 ②phpmyadminでSQL実行しているなら、データベースを選択後、SQLタブで実行 してみてください。
Chandler_Bing

2018/12/15 08:40

有難うございます。 SQL実行しております。ご指摘の通りsushiテーブルを選択している状態ですと機能しました。 しかしhistoryテーブルを選択している状態では実行してもエラーになります。なぜでしょうか。 またこの状態ではSQL文は正しいと言えますか。
Chandler_Bing

2018/12/15 08:45

SELECT COUNT(*) FROM (SELECT history.productid AS productid, history.tableNumber AS tableNumber, history.quantity AS quantity, history.time AS time, sushi.name AS name, sushi.price AS price, sushi.img AS img, sushi.intro AS intro FROM history INNER JOIN sushi ON (history.productid = sushi.id) WHERE history.tableNumber = 6 UNION ALL SELECT history.productid AS productid, history.tableNumber AS tableNumber, history.quantity AS quantity, history.time AS time, dessert.name AS name, dessert.price AS price, dessert.img AS img, dessert.intro AS intro FROM history INNER JOIN dessert ON (history.productid = dessert.id) WHERE history.tableNumber = 6) AS CONT; これで上手くいきました
guest

回答1

0

ベストアンサー

どんなエラーか書かないと分かりませんよ
パッと見ではサブクエリーにselectが無いようですが

SQL

1SELECT COUNT(*) FROM ( 2select -- ←ココ 3 history.productid AS productid, history.tableNumber AS tableNumber, history.quantity AS quantity, history.time AS time, 4 sushi.name AS name, sushi.price AS price, sushi.img AS img, sushi.intro AS intro 5 FROM history INNER JOIN sushi ON (history.productid = sushi.id) 6 WHERE history.tableNumber = ? 7 UNION ALL 8 SELECT history.productid AS productid, history.tableNumber AS tableNumber, history.quantity AS quantity, history.time AS time, 9 dessert.name AS name, dessert.price AS price, dessert.img AS img, dessert.intro AS intro 10 FROM history INNER JOIN dessert ON (history.productid = dessert.id) 11 WHERE history.tableNumber = ?)

投稿2018/12/15 07:15

sazi

総合スコア25195

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問