MySQL 连接表,其中表名是另一个表的字段

发布于 2024-12-26 10:28:05 字数 671 浏览 0 评论 0原文

我有5张桌子。 1 个主要的和 4 个附加的(它们有不同的列)。

  1. 对象
  2. obj_mobiles
  3. obj_tablets
  4. obj_computers

这是我的主表(对象)的结构。

ID |类型 |名称 |等等...

所以我想做的是将对象与其他(obj_mobiles,obj_tablets,...)表连接起来,具体取决于类型字段。 我知道我应该使用动态 SQL。但我无法办理手续。我认为它应该看起来像这样。

SELECT objects.type into @tbl FROM objects;
PREPARE stmnt FROM "SELECT * FROM objects AS object LEFT JOIN @tbl AS info ON object.id = info.obj_id"; 
EXECUTE stmnt;
DEALLOCATE PREPARE stmnt;

Aslo 伪代码

SELECT * FROM objects LEFT JOIN [objects.type] ON ... 

任何人都可以发布程序吗?另外我想要所有行而不仅仅是 1 行。 谢谢。

I have 5 tables. One primary and 4 additional (they have different columns).

  1. objects
  2. obj_mobiles
  3. obj_tablets
  4. obj_computers

Here is the structure of my main table (objects).

ID | type | name | etc...

So what I want to do is to join objects with other (obj_mobiles,obj_tablets,...) tables, depending on type field.
I know that I should use dynamic SQL. But I can't make procedure. I think it should look like something like this.

SELECT objects.type into @tbl FROM objects;
PREPARE stmnt FROM "SELECT * FROM objects AS object LEFT JOIN @tbl AS info ON object.id = info.obj_id"; 
EXECUTE stmnt;
DEALLOCATE PREPARE stmnt;

Aslo pseudo-code

SELECT * FROM objects LEFT JOIN [objects.type] ON ... 

Can anyone post procedure? Also I want to have all rows not just 1 row.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美男兮 2025-01-02 10:28:05

如果您想要所有行(批量输出)而不是一次一行,那么下面的代码应该很快,并且所有行的输出将包含所有列。

让我们考虑下面的表字段。
obj_mobiles - ID | obj_mobiles M1 | M2
obj_tablets-ID | obj_tablets T1 | T2
obj_计算机- ID | C1 | C2
对象- ID |类型 |名称 |等等,

Select objects.*, typestable.*
from (
    select ID as oID, "mobile" as otype, M1, M2, NULL T1, NULL T2, NULL C1, NULL C2 from obj_mobiles 
    union all
    select ID as oID, "tablet" as otype, NULL, NULL, T1, T2, NULL, NULL from obj_tablets 
    union all
    select ID as oID, "computer" as otype, NULL, NULL, NULL, NULL, C1, C2 from obj_computers) as typestable 
        left join objects on typestable.oID = objects.ID and typestable.otype = objects.type;

+------+--------------------+----------+------+----------+------+------+------+------+------+------+
| ID   | name               | type     | ID   | type     | M1   | M2   | T1   | T2   | C1   | C2   |
+------+--------------------+----------+------+----------+------+------+------+------+------+------+
|    1 | Samsung Galaxy s2  | mobile   |    1 | mobile   |    1 | Thin | NULL | NULL | NULL | NULL |
|    2 | Samsung Galaxy Tab | tablet   |    2 | tablet   | NULL | NULL | 0.98 |   10 | NULL | NULL |
|    3 | Dell Inspiron      | computer |    3 | computer | NULL | NULL | NULL | NULL | 4.98 | 1000 |
+------+--------------------+----------+------+----------+------+------+------+------+------+------+

表格创建如下。

mysql> create table objects (ID int, name varchar(50), type varchar (15));
Query OK, 0 rows affected (0.05 sec)
mysql> insert into objects values (1, "Samsung Galaxy s2", "mobile"), (2, "Samsung Galaxy Tab", "tablet"), (3, "Dell Inspiron", "computer");
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0


mysql> create table obj_mobiles (ID int, M1 int, M2 varchar(10));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into obj_mobiles values (1, 0.98, "Thin");
Query OK, 1 row affected (0.00 sec)


mysql> create table obj_tablets (ID int, T1 float, T2 int(10));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into obj_tablets values (2, 0.98, 10);
Query OK, 1 row affected (0.00 sec)


mysql> create table obj_computers (ID int, C1 float, C2 int(10));
Query OK, 0 rows affected (0.03 sec)
insert into obj_computers values (3, 4.98, 1000);

还要确认列的数据类型与原始列相同,结果将保存到表中并在下面检查数据类型。

create table temp_result as
Select objects.*, typestable.*
from (
    select ID as oID, "mobile" as otype, M1, M2, NULL T1, NULL T2, NULL C1, NULL C2 from obj_mobiles 
    union all
    select ID as oID, "tablet" as otype, NULL, NULL, T1, T2, NULL, NULL from obj_tablets 
    union all
    select ID as oID, "computer" as otype, NULL, NULL, NULL, NULL, C1, C2 from obj_computers) as typestable 
        left join objects on typestable.oID = objects.ID and typestable.otype = objects.type;

mysql> desc temp_result;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
| type  | varchar(15) | YES  |     | NULL    |       |
| oID   | int(11)     | YES  |     | NULL    |       |
| otype | varchar(8)  | NO   |     |         |       |
| M1    | int(11)     | YES  |     | NULL    |       |
| M2    | varchar(10) | YES  |     | NULL    |       |
| T1    | float       | YES  |     | NULL    |       |
| T2    | int(11)     | YES  |     | NULL    |       |
| C1    | float       | YES  |     | NULL    |       |
| C2    | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
11 rows in set (0.00 sec)

If you want all rows (bulk output) and not one row at a time, the below should be fast and also the output for all rows will contain all columns.

Lets consider below to be the fields of the tables.
obj_mobiles - ID | M1 | M2
obj_tablets - ID | T1 | T2
obj_computers - ID | C1 | C2
objects - ID | type | name | etc.,

Select objects.*, typestable.*
from (
    select ID as oID, "mobile" as otype, M1, M2, NULL T1, NULL T2, NULL C1, NULL C2 from obj_mobiles 
    union all
    select ID as oID, "tablet" as otype, NULL, NULL, T1, T2, NULL, NULL from obj_tablets 
    union all
    select ID as oID, "computer" as otype, NULL, NULL, NULL, NULL, C1, C2 from obj_computers) as typestable 
        left join objects on typestable.oID = objects.ID and typestable.otype = objects.type;

+------+--------------------+----------+------+----------+------+------+------+------+------+------+
| ID   | name               | type     | ID   | type     | M1   | M2   | T1   | T2   | C1   | C2   |
+------+--------------------+----------+------+----------+------+------+------+------+------+------+
|    1 | Samsung Galaxy s2  | mobile   |    1 | mobile   |    1 | Thin | NULL | NULL | NULL | NULL |
|    2 | Samsung Galaxy Tab | tablet   |    2 | tablet   | NULL | NULL | 0.98 |   10 | NULL | NULL |
|    3 | Dell Inspiron      | computer |    3 | computer | NULL | NULL | NULL | NULL | 4.98 | 1000 |
+------+--------------------+----------+------+----------+------+------+------+------+------+------+

The table are create as below.

mysql> create table objects (ID int, name varchar(50), type varchar (15));
Query OK, 0 rows affected (0.05 sec)
mysql> insert into objects values (1, "Samsung Galaxy s2", "mobile"), (2, "Samsung Galaxy Tab", "tablet"), (3, "Dell Inspiron", "computer");
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0


mysql> create table obj_mobiles (ID int, M1 int, M2 varchar(10));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into obj_mobiles values (1, 0.98, "Thin");
Query OK, 1 row affected (0.00 sec)


mysql> create table obj_tablets (ID int, T1 float, T2 int(10));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into obj_tablets values (2, 0.98, 10);
Query OK, 1 row affected (0.00 sec)


mysql> create table obj_computers (ID int, C1 float, C2 int(10));
Query OK, 0 rows affected (0.03 sec)
insert into obj_computers values (3, 4.98, 1000);

also to confirm the datatypes of the columns are same as the original columns, the result is saved into a table and datatypes are checked below.

create table temp_result as
Select objects.*, typestable.*
from (
    select ID as oID, "mobile" as otype, M1, M2, NULL T1, NULL T2, NULL C1, NULL C2 from obj_mobiles 
    union all
    select ID as oID, "tablet" as otype, NULL, NULL, T1, T2, NULL, NULL from obj_tablets 
    union all
    select ID as oID, "computer" as otype, NULL, NULL, NULL, NULL, C1, C2 from obj_computers) as typestable 
        left join objects on typestable.oID = objects.ID and typestable.otype = objects.type;

mysql> desc temp_result;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
| type  | varchar(15) | YES  |     | NULL    |       |
| oID   | int(11)     | YES  |     | NULL    |       |
| otype | varchar(8)  | NO   |     |         |       |
| M1    | int(11)     | YES  |     | NULL    |       |
| M2    | varchar(10) | YES  |     | NULL    |       |
| T1    | float       | YES  |     | NULL    |       |
| T2    | int(11)     | YES  |     | NULL    |       |
| C1    | float       | YES  |     | NULL    |       |
| C2    | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
11 rows in set (0.00 sec)
爱情眠于流年 2025-01-02 10:28:05

如果 objects 是父表,则意味着 objects.ID 是唯一的对象。正确的?所有其他项目(手机、平板电脑、计算机)都是对象的子项目,并且手机和平板电脑不能具有相同的 ID。如果是这样,使用这个简单的查询就足够了 -

SELECT * FROM objects o
  LEFT JOIN obj_mobiles m
    ON o.id = m.ID
  LEFT JOIN obj_tablets t
    ON o.id = t.ID
  LEFT JOIN obj_computers c
    ON o.id = c.ID

添加 WHERE 子句来过滤类型,例如:WHERE o.type = 'mobile'。

If objects is a parent table it means that objects.ID is a unique object. Right? All other items (mobiles, tablets, computers) are child ones for object and mobile and tablet cannot have the same ID. If so, it is enough to use this simple query -

SELECT * FROM objects o
  LEFT JOIN obj_mobiles m
    ON o.id = m.ID
  LEFT JOIN obj_tablets t
    ON o.id = t.ID
  LEFT JOIN obj_computers c
    ON o.id = c.ID

Add WHERE clause to filter types, e.g.: WHERE o.type = 'mobile'.

够钟 2025-01-02 10:28:05

使用 CONCAT 创建准备语句。
例如。

@sql = CONCAT('SELECT * FROM objects AS object LEFT JOIN ', @tbl, ' AS info ON object.id = info.obj_id');
PREPARE stmnt FROM @sql;
...

USE CONCAT to create your prepare statement.
eg.

@sql = CONCAT('SELECT * FROM objects AS object LEFT JOIN ', @tbl, ' AS info ON object.id = info.obj_id');
PREPARE stmnt FROM @sql;
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文