【问题标题】:phpmyadmin gives syntax error to join 2 tables if a column contains substring如果列包含子字符串,phpmyadmin 给出连接 2 个表的语法错误
【发布时间】:2021-03-05 18:28:16
【问题描述】:

“mytest_people”和“mytest_hobbies”:

  PK                 FK    <---  PK
+----+------+------+------+    +----+-----------------+
| Id | Name | Age  | H_ID |    | Id | Hobby           |
+----+------+------+------+    +----+-----------------+
| 0  | Pete | 100  | 0    |    | 0  | Reading News    |
| 1  | Mark | 5    | 1    |    | 1  | Reading Fiction |
| 2  | Bob  | 33   | 3    |    | 2  | Writing         |
| 3  | Lulu | 12   | 1    |    | 3  | Cooking         |
+----+------+------+------+    +----+-----------------+

这是我期望得到的:

+----+------+------+-----------------+
| Id | Name | Age  | Hobby           |
+----+------+------+-----------------+
| 0  | Pete | 100  | Reading News    |
| 1  | Mark | 5    | Reading Fiction |
| 3  | Lulu | 12   | Reading Fiction |
+----+------+------+-----------------+

这是日常用语/伪代码:

IN DATABASE "my_new_test_data_base"
SELECT COLUMNS "Id, Name, Age"
FROM TABLE "mytest_people"
WHERE COLUMN "H_ID"
    EQUALS COLUMN "Id"
    FROM TABLE "mytest_hobbies"
THEN JOIN COLUMN Hobby
FROM TABLE mytest_hobbies
ON CONDITION Hobby VALUES
    CONTAIN 'Reading%' SUBSTRING;

这是我对 SQL 代码的最新尝试:

USE my_new_test_data_base
SELECT mytest_people.Id, mytest_people.Name, mytest_people.Age FROM mytest_people
INNER JOIN mytest_hobbies (`Hobby`)
ON mytest_people.H_ID = mytest_hobbies.Id
WHERE `Hobby` LIKE 'Reading%';

这是我得到的错误:

#1064 - 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在 'SELECT mytest_people.Id, mytest_people.Name, mytest_people.Age 附近使用的正确语法 FROM mytest_peop' 在第 2 行

【问题讨论】:

    标签: mysql join phpmyadmin syntax-error inner-join


    【解决方案1】:

    如果你喜欢少打字:

    SELECT p.Id
         , p.Name
         , p.Age 
         , h.hobby
      FROM my_new_test_data_base.mytest_people p
      JOIN my_new_test_data_base.mytest_hobbies h
        ON p.H_ID = h.Id
     WHERE h.Hobby LIKE 'Reading%';
    

    【讨论】:

      【解决方案2】:

      一些小错误,每个命令都必须以分号结尾

      别名不属于括号

      USE my_new_test_data_base;
      SELECT mytest_people.Id, mytest_people.Name, mytest_people.Age ,mytest_hobbies.`Hobby`
      FROM mytest_people
      INNER JOIN mytest_hobbies 
      ON mytest_people.H_ID = mytest_hobbies.Id
      WHERE mytest_hobbies.`Hobby` LIKE 'Reading%';
      

      【讨论】:

      • 为此它会引发错误:#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT mytest_people.Id, mytest_people.Name, mytest_people.Age, mytest_hobbies.' at line 2。我使用 Xampp v3.2.3。编译 Mar.7.2019.. 以及 MariaDB 附带的任何版本。我是新手,所以我不知道问题是什么。
      • 这没问题,所以看到dbfiddle.uk/…所以你添加了一个错字
      猜你喜欢
      • 2017-12-19
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 2018-06-12
      相关资源
      最近更新 更多