简体   繁体   中英

Selecting from two tables and a where condition

I have two tables:

Images:
id | name | width

Extra:
id | width | extra

How can I select all from images and for each row, select 'extra' from the extra table where images.width is <= extra.width?

Eg

Images:
id | name | width
1  | a.jpg| 100
2  | b.jpg| 200
3  | c.jpg| 5
Extra:
id | width | extra
1  | 100   | 'test'
2  | 200   | 'normal'
3  | 50    | 'better'

So:

  • a.jpg would have an extra of 'test'.
  • b.jpg would have an extra of 'normal'.
  • c.jpg would have an extra of 'better.

Try this query

SELECT *
FROM Images JOIN Extra ON Images.width<=Extra.width

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM