duanran6441 2017-11-16 15:55
浏览 54
已采纳

Mysql内部连接根据值的多个表

I have to create a database of real state for sale. Problem is the kind of properties: if I have a house, I have a kind of description, but if it is just a land, the descriptions does not need to include number of bathrooms, just the area, front range, etc.

So I did a table with generic data about the element (imoveis) with address, price etc.. Than I created categories of elements (imoveis_categs). I did 5 categories, for each category, there will be a table (e.g.: imoveis_descr2) with the specific features of the kind.

To enter data it's easy, but to list my data, I will need to perform query select to find those elements according with some filters. In PHP will be easy to solve, but I am wondering about the performance for a large amount of data and users requests. Better solve it by SQL commands, thought. But mySQL is not my area, I imagine something like this to start...

 SELECT * FROM imoveis INNER JOIN imoveis_descr(imoveis.categ) ...

The "categ" field of imoveis point to the right description table. It is possible to do something like this? Is there another way more appropriate or efficient to do it?

EDIT: I tryed to clarify with an example... EDIT2: I corrected the example, the column "rooms" will be the same. The fields are not exclusives, both apartment and house categories have number of rooms.

Table imoveis
id  categ   title            price       address ...
1   2       The House        $ 1000000   Somestreet 77
2   1       An Appartment    $ 500000    Somewhere 11
3   4       A Land           $ 250000    Nowhere 33

Table imoveis_descr1
idImovel    rooms   area    floor   ...
2           2       70      5

Table imoveis_descr2
idImovel    rooms fieldArea   constrArea ...
1           3      120        80

Table imoveis_descr4
idImovel    area    width   height ...
3           2640    22      120

Result
id   categ    title       price     address   rooms fieldArea  constrArea    area    floor        area   width  height
1      2     The House    $ 1000000 Somestreet 77   3   120 80  null    null    null    null    null
2   1   An Appartment   $ 500000    Somewhere 11    2   null    null    70  5   null    null    null
3   4   A Land  $ 250000    Nowhere 33  null    null    null    null    null    2640    22  120
  • 写回答

1条回答 默认 最新

  • duan1979768678 2017-11-16 18:10
    关注

    Some of your field-names are duplicated in your results (e.g. "rooms"), wouldn't it be better to merge those fields with COALESCE (as they appear mutually exclusive)?

    As the tables your refer to are mutually exclusive, you will never get the required results with an INNER JOIN, you'll need an OUTER JOIN:

    SELECT
          i.id,
          i.categ,
          i.title,
          i.price,
          i.address,
          COALESCE(i1.rooms, i2.rooms) AS rooms,
          i2.fieldArea,
          i2.constrArea
          COALESCE(i1.area, i3.area, ...) AS area,
          ...
    FROM  imoveis AS i
          LEFT OUTER JOIN imoveis_descr1 AS i1 ON i1.idImovel = i.id
          LEFT OUTER JOIN imoveis_descr2 AS i2 ON i2.idImovel = i.id
          LEFT OUTER JOIN imoveis_descr3 AS i3 ON i3.idImovel = i.id
          ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: