SQL Query not working perfectly

ANJYR

I have three tables Chain, Hotel and Shell. Hotel table link with Chain table and shell table link with both of tables.

Table Structure :---

Chain //Level 2

ChainID    |    ChainName

Hotel //Level 3

HotelID    |    HotelName    |    ChainID

Shell

ShellID    |    ShellName    |    Chain_Hotel_ID    |    Level

In Shell Table, Chain_Hotel_ID column hold (Chain and Hotel) table ChainID and HotelID and Level Column Hold Int Value 2 and 3. Chain consider as 2 and Hotel 3

From these table i want

ShellID    |    ShellName    |    ChainName

I write a query for that but it not working as I want

select 
   _gs.ShellID, _gs.ShellName, _gc.ChainName 
from 
   Shell _gs, Chain _gc
where 
   _gc.ChainID in (select _lh.ChainID 
                   from Shell _ls,Hotel _lh 
                   where _lh.HotelID = _ls.Chain_Hotel_ID and _ls.Level = 3) 
   and _gs.Level = 3

Sample Table Data

Chain

ChainID    |    ChainName
   1       |    Sarovar
   2       |    Taj Group
   3       |    Oberoi

Hotel

HotelID    |    HotelName    |    ChainID
   1       |   RadhaRegent   |      1
   2       |   SarovarPortic |      1
   3       |   LaxmiVilas    |      1
   4       |   Taj Mumbai    |      2
   5       |   Taj Delhi     |      2
   6       |   Oberoi Delhi  |      3

Shell

ShellID    |    ShellName    |    Chain_Hotel_ID    |    Level
   1       |   Shell1        |        1             |      2
   2       |   Shell2        |        1             |      2
   3       |   Shell3        |        2             |      3
   4       |   Shell4        |        3             |      2
   5       |   Shell5        |        2             |      3
   6       |   Shell6        |        3             |      3
   7       |   Shell7        |        4             |      3

Output

ShellID    |    ShellName    |    ChainName    
   3       |   Shell3        |      Sarovar         
   5       |   Shell5        |      Sarovar         
   6       |   Shell6        |      Sarovar         
   7       |   Shell7        |      Taj Group   
   3       |   Shell3        |      Sarovar         
   5       |   Shell5        |      Sarovar         
   6       |   Shell6        |      Sarovar         
   7       |   Shell7        |      Taj Group

I got repeated data

Dudi Konfino

Is this what you need?

IF LEVEL=3
BEGIN
SELECT DISTINCT ShellID,  ShellName,  ChainName
FROM HOTEL JOIN SHELL
ON HOTEL.CHAINID=SHELL.CHAIN_HOTEL_ID
JOIN 
CHAIN ON CHAIN.CHAINID=HOTEL.CHAINID
END

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQL query not functioning correctly, getting no output with a perfectly fine query

分類Dev

SQL query condition not working

分類Dev

Not sure how to get this working with respect a SQL query

分類Dev

SQL multi table query working slow

分類Dev

Why is my SQL "INSERT INTO" query not working?

分類Dev

Query with multiple parameters not working in SQL Server

分類Dev

Error when profiling an otherwise perfectly working multiprocessing python script with cProfile

分類Dev

SQL Grammar Exception-Invalid Character, but query working fine

分類Dev

SQL Server Query not working when executed from PHP

分類Dev

SQL Query not working using alias, but does work with indexing

分類Dev

Can't find location of libopencv_core.so.3.4.0 even though program is installed and working perfectly

分類Dev

Using Dropbox as git working directory across multiple machines - commits don't sync perfectly

分類Dev

MySQL query in PHP is not working

分類Dev

simple insert query not working

分類Dev

Query MAX function not working

分類Dev

SQLite query > operator not working

分類Dev

LIMIT not working on complex query

分類Dev

nslookup query not working in Ubuntu

分類Dev

XML Query with a variable not working

分類Dev

php UPDATE not working query

分類Dev

mysql insert into query not working

分類Dev

LIKE query not working as expected

分類Dev

Psycopg2 SQL Statment is working from the query Builder but not from Python

分類Dev

Sql query with Joins into Linq query

分類Dev

Translate SQL Query into LINQ Query

分類Dev

Grouping by Name in Query Formula not working

分類Dev

Firestore "where" query not working as expected

分類Dev

Yii update query not working as expected

分類Dev

CSS clear not working with Media Query

Related 関連記事

  1. 1

    SQL query not functioning correctly, getting no output with a perfectly fine query

  2. 2

    SQL query condition not working

  3. 3

    Not sure how to get this working with respect a SQL query

  4. 4

    SQL multi table query working slow

  5. 5

    Why is my SQL "INSERT INTO" query not working?

  6. 6

    Query with multiple parameters not working in SQL Server

  7. 7

    Error when profiling an otherwise perfectly working multiprocessing python script with cProfile

  8. 8

    SQL Grammar Exception-Invalid Character, but query working fine

  9. 9

    SQL Server Query not working when executed from PHP

  10. 10

    SQL Query not working using alias, but does work with indexing

  11. 11

    Can't find location of libopencv_core.so.3.4.0 even though program is installed and working perfectly

  12. 12

    Using Dropbox as git working directory across multiple machines - commits don't sync perfectly

  13. 13

    MySQL query in PHP is not working

  14. 14

    simple insert query not working

  15. 15

    Query MAX function not working

  16. 16

    SQLite query > operator not working

  17. 17

    LIMIT not working on complex query

  18. 18

    nslookup query not working in Ubuntu

  19. 19

    XML Query with a variable not working

  20. 20

    php UPDATE not working query

  21. 21

    mysql insert into query not working

  22. 22

    LIKE query not working as expected

  23. 23

    Psycopg2 SQL Statment is working from the query Builder but not from Python

  24. 24

    Sql query with Joins into Linq query

  25. 25

    Translate SQL Query into LINQ Query

  26. 26

    Grouping by Name in Query Formula not working

  27. 27

    Firestore "where" query not working as expected

  28. 28

    Yii update query not working as expected

  29. 29

    CSS clear not working with Media Query

ホットタグ

アーカイブ