I'd really appreciate some help with an SQL query across tables. I realise this sort of thing is asked constantly, but I can't find a similar enough question to make sense of the answers.
I want to select rows from table_A
that have a corresponding tag in table_B
.
So, for example, " select rows from table_a
which are tagged 'chair' " would return table_C
.
Also, id
is a unique in table_a
, and not in table_b
.
table_A: table_B: table_C:
id object id tag id object
1 lamp 1 furniture 3 stool
2 table 2 furniture 4 bench
3 stool 3 furniture
4 bench 4 furniture
4 chair
3 chair
Alternatively, is there a better way to organise the data?
You should make tags their own table with a linking table.
I have a similar problem (at least I think it is similar). In one of the replies here the solution is as follows:
That WHERE clause I would like to be:
or, in my specific case:
More detailed:
Table A carries status information of a fleet of equipment. Each status record carries with it a start and stop time of that status. Table B carries regularly recorded, timestamped data about the equipment, which I want to extract for the duration of the period indicated in table A.
The simplest solution would be a correlated sub select:
Alternatively you could join the tables and filter the rows you want:
You should profile both and see which is faster on your dataset.