簡體   English   中英

如何從多個表中獲取數據

[英]how to fetch data from multiple tables

因此,我對我的數據庫進行了規范化,而獲取或更新表中的記錄對我來說似乎相當復雜。 我有5個表格( detailscountrymaterialsvendor_countriesvendor_materials )。 實際上, "details"是我的主要表。

  • country表有兩列( idname )。
  • materials表也​​有兩列( idname )。
  • details表包含有關供應商的人員信息( nameemailphoneaddress )。

一個供應商可以有多個國家和地區,因此我又創建了兩個表vendor_countriesvendor_materials ,它們的列是( didcid ),( didmid )。 "did"是來自details表和cid的每個供應商的ID, mid是來自國家和材料表的國家ID和材料ID。

現在,我想獲取供應商及其國家和材料。

這是我的詳細信息表,vendor_countries和vendor_materials表

詳情表 供應商材料 供應商國家

到目前為止,我進行了查詢,如下所示:

select dt.vendor,
       dt.email,
       dt.address,
       c.country,
       m.material,
       c.country
  from country c
       inner join vendor_countries vc on (c.id = vc.cid)
       right join details dt on (dt.id = vc.did)
       left join vendor_materials vm on dt.id = vm.did
       left join material m on vm.mid = m.id

我得到這樣的結果:

結果

這是不對的,應該分成3行,因為“ ali”供應商在3個國家/地區和3種材料下工作。 我正在尋找一些不會降低我的頁面速度的智能解決方案。 提前致謝。

在沒有關於表模式的任何響應的情況下,我嘗試根據上述說明重新創建表-在每個表前加上v_前綴,以便我可以將該表分組, didvid (vendor id)的方式進行引用,並填充我相信是問題的樣本數據

mysql> describe v_country;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(50)      | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+


mysql> describe v_details;
+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| vendor  | varchar(50)      | YES  |     | NULL    |                |
| email   | varchar(50)      | YES  |     | NULL    |                |
| phone   | varchar(50)      | YES  |     | NULL    |                |
| address | varchar(50)      | YES  |     | NULL    |                |
+---------+------------------+------+-----+---------+----------------+


mysql> describe v_materials;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(50)      | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+


mysql> describe v_vendor_materials;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| vid   | int(10) unsigned | NO   | MUL | 0       |                |
| mid   | int(10) unsigned | NO   | MUL | 0       |                |
+-------+------------------+------+-----+---------+----------------+


mysql> describe v_vendor_countries;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| vid   | int(10) unsigned | NO   | MUL | 0       |                |
| mid   | int(10) unsigned | NO   | MUL | 0       |                |
+-------+------------------+------+-----+---------+----------------+






mysql> select * from v_country;
+----+----------+
| id | name     |
+----+----------+
|  1 | pakistan |
|  2 | India    |
|  3 | Iran     |
+----+----------+


mysql> select * from v_details;
+----+--------+------------------+---------------+-----------+
| id | vendor | email            | phone         | address   |
+----+--------+------------------+---------------+-----------+
|  1 | harris | harris@gmail.com | 0141 236 4523 | nowhere   |
|  2 | Boris  | boris@gmail.com  | 0141 451 7845 | somewhere |
|  3 | Doris  | doris@gmail.com  | 0141 353 7845 | anywhere  |
+----+--------+------------------+---------------+-----------+


mysql> select * from v_materials;
+----+---------+
| id | name    |
+----+---------+
|  1 | ceramic |
|  2 | iron    |
|  3 | plastic |
+----+---------+


mysql> select * from v_vendor_materials;
+----+-----+-----+
| id | vid | mid |
+----+-----+-----+
|  1 |   1 |   1 |
|  2 |   2 |   2 |
|  3 |   3 |   3 |
+----+-----+-----+


mysql> select * from v_vendor_countries;
+----+-----+-----+
| id | vid | mid |
+----+-----+-----+
|  1 |   1 |   1 |
|  2 |   2 |   2 |
|  3 |   3 |   3 |
+----+-----+-----+





mysql> select * from v_details d
          left outer join v_vendor_materials vm on vm.vid=d.id
          left outer join v_vendor_countries vc on vc.vid=d.id
          left outer join v_materials m on m.id=vm.id
          left outer join v_country c on c.id=vc.id;

+----+--------+------------------+---------------+-----------+------+------+------+------+------+------+------+---------+------+----------+
| id | vendor | email            | phone         | address   | id   | vid  | mid  | id   | vid  | mid  | id   | name    | id   | name     |
+----+--------+------------------+---------------+-----------+------+------+------+------+------+------+------+---------+------+----------+
|  1 | harris | harris@gmail.com | 0141 236 4523 | nowhere   |    1 |    1 |   1  |    1 |    1 |    1 |    1 | ceramic |    1 | pakistan |
|  2 | Boris  | boris@gmail.com  | 0141 451 7845 | somewhere |    2 |    2 |   2  |    2 |    2 |    2 |    2 | iron    |    2 | India    |
|  3 | Doris  | doris@gmail.com  | 0141 353 7845 | anywhere  |    3 |    3 |   3  |    3 |    3 |    3 |    3 | plastic |    3 | Iran     |
+----+--------+------------------+---------------+-----------+------+------+------+------+------+------+------+---------+------+----------+

或者,更具選擇性的查詢

select 
    d.`id` as `vid`,
    d.`vendor`,
    d.`email`,
    d.`phone`,
    d.`address`,
    m.`name` as `material`,
    c.`name` as `country`
    from v_details d
    left outer join v_vendor_materials vm on vm.vid=d.id
    left outer join v_vendor_countries vc on vc.vid=d.id
    left outer join v_materials m on m.id=vm.id
    left outer join v_country c on c.id=vc.id;


+-----+--------+------------------+---------------+-----------+----------+----------+
| vid | vendor | email            | phone         | address   | material | country  |
+-----+--------+------------------+---------------+-----------+----------+----------+
|   1 | harris | harris@gmail.com | 0141 236 4523 | nowhere   | ceramic  | pakistan |
|   2 | Boris  | boris@gmail.com  | 0141 451 7845 | somewhere | iron     | India    |
|   3 | Doris  | doris@gmail.com  | 0141 353 7845 | anywhere  | plastic  | Iran     |
+-----+--------+------------------+---------------+-----------+----------+----------+

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM