MySQL Query 2 inner join? - mysql

I have a problem with my sql query in mysql. in sqlite3 and sql server all works.
SELECT `buildings`.*
FROM `buildings`
INNER JOIN "floors"
ON "floors"."building_id" = "buildings"."id"
INNER JOIN "spaces"
ON "spaces".floor_id = "floors".id
maybe i need to process on other way in mysql?
thanks

MySQL treat words in quotes ("floors") as strings, so those values are NOT used as table/field names. Try
SELECT ...
...
INNER JOIN floors ON floors.building_id = buildings.id
INNER JOIN spaces ON spaces.floor_id = floors.id
instead. Backticks around table/field names are required ONLY when the table/field name is a reserved word. buildings is not a reserved word, so no backticks are necessary.

ANSI_QUOTES may not be enabled. From the manual ref:
If the ANSI_QUOTES SQL mode is enabled, it is also permissible to quote identifiers within double quotation marks:
mysql> CREATE TABLE "test" (col INT);
ERROR 1064: You have an error in your SQL syntax...
mysql> SET sql_mode='ANSI_QUOTES';
mysql> CREATE TABLE "test" (col INT);
Query OK, 0 rows affected (0.00 sec)
Make sure ANSI_QUOTES is enabled, or just stick with the traditional backtick (`)
This is also, from your vague question, assuming I have the correct problem to chase.

SELECT `buildings`.*
FROM `buildings`
INNER JOIN `floors`
ON `floors`.`building_id` = `buildings`.`id`
INNER JOIN `spaces`
ON `spaces`.`floor_id` = `floors`.`id`
or
SELECT buildings.*
FROM buildings
INNER JOIN floors
ON floors.building_id = buildings.id
INNER JOIN spaces
ON spaces.floor_id = floors.id
Do not use : ".
This is for String in MySql.

Related

Get all data from 3 table in MySQL

I want to get all the data through the username from the 3 tables but I can not.
$username = $_GET['username'];
$res=$conn->query("select * from posts,tbl_view,tbl_user where posts.idpost=tbl_view.post_id AND
username='$username'");
Seems you missed the condition for join tbl_user with post.
Assuming your fk column is user_id, your query could be:
select *
from posts
INNER JOIN tbl_view ON posts.idpost=tbl_view.post_id
INNER JOIN tbl_user ON posts.user_id = tbl_user.id
WHERE tbl_user.username='$username'
You should not use the old join syntax based on comma separated table's name and where condition but use the explicit join syntax
And you should avoid the use of php var in sql. You should use prepared statement and parameter binding (to avoid SQL injection risk).

redash SQL query inner join with query

I am using redash to display data and am struggling to figure out how to show only specific images for this join query.
The problem is with my last AND condition which seems to break the query (AND "images"."imageable_type" = "BrandProfile"). Below is the error message I am receiving.
Error running query: column "BrandProfile" does not exist LINE 31: ...s"."height" = 760 AND "images"."imageable_type" = "BrandProf... ^
SELECT "brand_profiles"."company_name",
"users"."full_name",
"brand_profiles"."location",
"brand_profiles"."company_website",
"brand_profiles"."description",
"images"."processed_url"
FROM "brand_profiles"
INNER JOIN "users" ON "users"."id" = "brand_profiles"."user_id"
INNER JOIN "images" ON "images"."imageable_id" = "brand_profiles"."id" AND "images"."height" = 760 AND "images"."imageable_type" = "BrandProfile"
WHERE "brand_profiles"."deleted_at" IS NULL
AND "brand_profiles"."marketplace" = true
Could you try to replace "BrandProfile" with 'BrandProfile'. Also would recommend to read the post When to use single quotes, double quotes, and backticks in MySQL that explains how behaviour of ANSI_QUOTES mode.

MySql update syntax error from inner join

UPDATE
`universities`
SET
`universities`.countryid = `countries`.id,
FROM
`universities`
INNER JOIN
`countries`
ON
`universities`.country = `countries`.name
When I try to run the sql statements above via PhpMyAdmin, it would give syntax errors. I wrote the statements based on this answer.
This is the correct syntax in MySQL:
UPDATE universities u JOIN
countries c
ON u.country = c.name
SET u.countryid = c.id;
In addition, I introduced table aliases (so the query is easier to write and to read) and removed an extraneous comma.

RODBC sqlQuery: name with special character #

I am using RODBC to merge two tables in ACCESS. Here are my codes:
qry <- "SELECT * FROM
table1 LEFT OUTER JOIN table2
ON table1.Ref# = table2.Ref# "
result <- sqlQuery(connection, qry)
str(result)
Which returns the following error message:
"42000 -3100 [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression
I believe it's because I didn't use "table1.Ref# = table2.Ref#" correctly. With the special character, how can I modify this query? Thank you very much in advance.
Try wrapping the field names with special characters inside square brackets.
SELECT
*
FROM
table1
LEFT JOIN
table2
ON
[table1].[Ref#] = [table2].[Ref#]
Also Access does not support OUTER Join, so simply try LEFT Join.

MySQL join error

The following query gives me an error in phpmyadmin. It looks syntactically correct to me, and the table/column names match up accordingly. I have tried a number of variations (quoting table names, using as, etc) with no luck.
SELECT *
FROM GROUP
INNER JOIN GROUP_MEMBER ON GROUP.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';
Error I'm getting:
1064 - 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 'GROUP INNER JOIN GROUP_MEMBER ON GROUP.group_id = GROUP_MEMBER.group_id WHERE ' at line 2
"group" is a sql-keyword, you need to surround it with backticks if you want to use it as tablename.
GROUP is a reserved word in SQL so it's a bad choice for a table name. If you surround it with backticks it might work but I'd really recommend changing that table name.
SELECT *
FROM `GROUP`
INNER JOIN GROUP_MEMBER ON `GROUP`.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';
This is not PHPMyAdmin-specfiic error. The problem you have is using a table name GROUP that matches a MySQL reserved word. If you insist on using such a problematic table name, you need to enclose it with backticks anywhere you might use it.
SELECT *
FROM `GROUP`
INNER JOIN GROUP_MEMBER ON `GROUP`.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';