SQL - Searching in a table based on information in another table and returning multiple results

Carl M

I have one table 'trace' which contains the following information:

|  galaxyId  |  lastProgenitorId  |
-----------------------------------
|  0         |  27                |
|  2890      |  3001              |
|  ...       |  ...               |
|  189279    |  190056            |
-----------------------------------

And I have another table 'main' which includes the following:

|  galaxyId  |  stellarMass  |  umag  |  imag  |
------------------------------------------------
|  25        |  10.5         |  -21   |  -22   |
|  2901      |  10.8         |  -23   |  -21   |
|  3000      |  10.0         |  -22   |  -21   |
|  ...       |  ...          |  ...   |  ...   |
|  200000    |  10.1         |  -22   |  -22   |
------------------------------------------------

What I wish to do is, for each trace.galaxyId, retrieve ALL the corresponding rows in maintable where the following condition is met:

main.galaxyId BETWEEN trace.galaxyId AND trace.lastProgenitorId

This could return multiple results, such that the results would be (from the example above):

|  trace.galaxyId  |  main.galaxyId  |  main.umag  |  main.imag  |
------------------------------------------------------------------
|  0               |  25             |  -21        |  -22        |
|  2890            |  2901           |  -23        |  -21        |
|  2890            |  3000           |  -22        |  -21        |
------------------------------------------------------------------ 

I feel like this is an easy one but my brain just can't see how to do this! I feel like PARTITION BY is needed? Any help greatly appreciated. Thanks in advance.

Paul Grimshaw

Use a join:

SELECT 
    t.GalaxyId As TraceGalaxyId,
    m.GalaxyId as MainGalaxyId,
    m.umag,
    m.imag 
FROM trace
JOIN main m 
ON m.galaxyId BETWEEN t.GalaxyId AND t.lastProgenitorId AND m.snap = 'Specific Value'

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to update table multiple times based on another table

분류에서Dev

Returning null values with multiple table in a query

분류에서Dev

Sql copying a data from table to another table

분류에서Dev

Update MySQL table based on results for joining to tables

분류에서Dev

Limit results based on the dates of a related table

분류에서Dev

Insert another hierarchy into a sql table

분류에서Dev

Insert selected value to table from another table multiple times

분류에서Dev

insert multiple rows mysql from another table

분류에서Dev

See if multiple entries exist in another table

분류에서Dev

Excel - sum values based on a column that match another column in another table

분류에서Dev

Updating sql table with results from left join (sql server)

분류에서Dev

MySql multiple select from two tables and join their results to a third table

분류에서Dev

Multiple sql joins from same reference table

분류에서Dev

SQL how to use multiple joins on the same table

분류에서Dev

SQL Multiple Joins on same table with where clause

분류에서Dev

SQL Server : convert multiple XML to table

분류에서Dev

Update statement to set a column based the maximum row of another table

분류에서Dev

How to do autoincrement based on last value from another table?

분류에서Dev

PARTITION BY multiple column while inserting data from another table

분류에서Dev

MYSQL select count of multiple ids occurring in another table

분류에서Dev

MySql select all rows in one table based on MAX value in another table

분류에서Dev

new variable in data.table R based on multiple criteria

분류에서Dev

How to print a table of information in Java

분류에서Dev

Merge value of two colums and insert in to another table in ms sql

분류에서Dev

SQL COUNT to count all the numbers from another table

분류에서Dev

SQL COUNT to count all the numbers from another table

분류에서Dev

SQL server : Inserting/updating missing data from one table to another

분류에서Dev

C# Linq CopyToDataTable based of Linq To Sql table class structure

분류에서Dev

SQL SELECT Data from other table based on column value

Related 관련 기사

  1. 1

    How to update table multiple times based on another table

  2. 2

    Returning null values with multiple table in a query

  3. 3

    Sql copying a data from table to another table

  4. 4

    Update MySQL table based on results for joining to tables

  5. 5

    Limit results based on the dates of a related table

  6. 6

    Insert another hierarchy into a sql table

  7. 7

    Insert selected value to table from another table multiple times

  8. 8

    insert multiple rows mysql from another table

  9. 9

    See if multiple entries exist in another table

  10. 10

    Excel - sum values based on a column that match another column in another table

  11. 11

    Updating sql table with results from left join (sql server)

  12. 12

    MySql multiple select from two tables and join their results to a third table

  13. 13

    Multiple sql joins from same reference table

  14. 14

    SQL how to use multiple joins on the same table

  15. 15

    SQL Multiple Joins on same table with where clause

  16. 16

    SQL Server : convert multiple XML to table

  17. 17

    Update statement to set a column based the maximum row of another table

  18. 18

    How to do autoincrement based on last value from another table?

  19. 19

    PARTITION BY multiple column while inserting data from another table

  20. 20

    MYSQL select count of multiple ids occurring in another table

  21. 21

    MySql select all rows in one table based on MAX value in another table

  22. 22

    new variable in data.table R based on multiple criteria

  23. 23

    How to print a table of information in Java

  24. 24

    Merge value of two colums and insert in to another table in ms sql

  25. 25

    SQL COUNT to count all the numbers from another table

  26. 26

    SQL COUNT to count all the numbers from another table

  27. 27

    SQL server : Inserting/updating missing data from one table to another

  28. 28

    C# Linq CopyToDataTable based of Linq To Sql table class structure

  29. 29

    SQL SELECT Data from other table based on column value

뜨겁다태그

보관