How to retrieve data from two MySQL tables with multiple associations - mysql

I got these two tables where one table is having multiple foreign keys to the second table.
Table rankings
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| search_text | varchar(255) | YES | MUL | NULL | |
| first_item_id | bigint(20) | YES | MUL | NULL | |
| second_item_id | bigint(20) | YES | MUL | NULL | |
| third_item_id | bigint(20) | YES | MUL | NULL | |
| forth_item_id | bigint(20) | YES | MUL | NULL | |
+----------------+--------------+------+-----+---------+----------------+
Table item
+---------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| item_code | varchar(255) | YES | MUL | NULL | |
+----------------+--------------+------+-----+---------+---------------------------+
One ranking record may have multiple association to item table using first_item_id, second_item_id, third_item_id or forth_item_id fields. I want to retrieve ranking records with the corresponding item_code instead of the item.id. What would be the most efficient way to do this if i have a big number of data?
PS: There are 10 associations to the item.id as first_item_id ... tenth_item_id. Im using Rails ActiveRecord ORM. Any workaround with that also fine.
Sample data ranking
SELECT id,search_text,first_item_id as first,second_item_id as second,third_item_id as third,forth_item_id as forth from rankings limit 10;
+----+-------------+-------+--------+-------+-------+
| id | search_text | first | second | third | forth |
+----+-------------+-------+--------+-------+-------+
| 1 | test 1 | 1 | 2 | 3 | 4 |
| 2 | test 2 | 1 | 2 | 3 | 4 |
| 3 | test 3 | 1 | 2 | 3 | 4 |
| 4 | test 4 | 1 | 2 | 3 | 4 |
+----+-------------+-------+--------+-------+-------+
Sample item data
SELECT id,item_code from items limit 5;
+--------+------------+
| id | item_code |
+--------+------------+
| 1 | 125659 |
| 2 | 125660 |
| 3 | 125661 |
| 4 | 125662 |
+--------+------------+
Expected data
+----+-------------+-------+--------+-------+-------+
| id | search_text | first | second | third | forth |
+----+-------------+-------+--------+-------+-------+
| 1 | test 1 | 125659| 125660 | 125661| 125662|
| 2 | test 2 | 125659| 125660 | 125661| 125662|
| 3 | test 3 | 125659| 125660 | 125661| 125662|
| 4 | test 4 | 125659| 125660 | 125661| 125662|
+----+-------------+-------+--------+-------+-------+

Joining the table multiple times (even many, many times) should not be a problem, as you are joining on the primary key, i.e. you have an index that will be used.
select
r.id,
r.search_text,
i1.item_code as item_code_1,
i2.item_code as item_code_2,
i3.item_code as item_code_3,
i4.item_code as item_code_4
from rankings r
left join item i1 on i1.id = r.first_item_id
left join item i2 on i2.id = r.second_item_id
left join item i3 on i3.id = r.third_item_id
left join item i4 on i4.id = r.forth_item_id
order by r.id;
I am using outer joins here, because all your item columns are nullable.

Related

MySQL database : how to write SQL for several tables

I need to create a view for client shipment documents which contains next info:
good name,
good count
good price.
Price may be different from one purchase agreement to other. For each purchase agreement may exists several shipments. Each good item in client_shipment_details may has serial number, but client_shipment document
keeps just total count of such kind of good.
This is my tables:
purchase_agreement;
+------------------------------+--------------+------+-----+---------+-------- --------+
| Field | Type | Null | Key | Default | Extra |
+------------------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| purchase_agreement_status_id | bigint(20) | YES | MUL | NULL | |
+------------------------------+--------------+------+-----+---------+----------------+
purchase_agreement_details;
+-----------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| count | int(11) | NO | | NULL | |
| price | float | NO | | NULL | |
| purchase_agreement_id | bigint(20) | YES | MUL | NULL | |
| good_id | bigint(20) | YES | MUL | NULL | |
+-----------------------+------------+------+-----+---------+----------------+
goods;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
client_shipment;
-----------------------+--------------+------+-----+---------+---------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| jhi_date | datetime | YES | | NULL | |
| purchase_agreement_id | bigint(20) | YES | MUL | NULL | |
+-----------------------+--------------+------+-----+---------+----------------+
client_shipment_details;
+--------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
|
| serial_number | varchar(255) | YES | | NULL | |
| client_shipment_id | bigint(20) | YES | MUL | NULL | |
| good_id | bigint(20) | YES | MUL | NULL | |
+--------------------+--------------+------+-----+---------+----------------+
This is my query :
SELECT
temp1.cs_id, temp1.good_id, temp1.count, temp1.price,good.name
FROM
(
SELECT csd.cs_id AS cs_id, csd.good_id AS good_id, csd.count AS count, pad.price AS price
FROM
(
SELECT client_shipment_id AS cs_id, good_id, count(*) AS count
FROM client_shipment_details
GROUP BY client_shipment_id, good_id
) AS csd
LEFT JOIN
(
SELECT pad.good_id AS good_id, pad.price AS price, cs.id AS cs_id
FROM purchase_agreement_details pad
JOIN client_shipment cs ON pad.purchase_agreement_id = cs.purchase_agreement_id
) AS pad ON csd.good_id = pad.good_id
GROUP BY cs_id,good_id
) AS temp1
JOIN goods ON temp1.good_id = good.id
ORDER BY cs_id,good_id;
Some test data:
insert into purchase_agreement values
(1,1),
(2,1);
insert into purchase_agreement_details values
(1,2,100,1,1),
(2,4,200,1,2),
(3,6,300,1,3),
(4,7,400,1,4),
(5,3,110,2,1),
(6,8,210,2,2),
(7,4,310,2,3),
(8,5,410,2,4);
insert into goods values
(1,"desk"),
(2,"sofa"),
(3,"chair"),
(4,"bed");
insert into client_shipment values
(1,'2019-02-21 09:57:00',1),
(2,'2019-02-21 09:57:00',1),
(3,'2019-02-21 09:57:00',2),
(4,'2019-02-21 09:57:00',2);
insert into client_shipment_details values
(1,"1-0001",1,1),
(2,"1-0002",1,1),
(3,"2-0001",1,2),
(4,"2-0002",1,2),
(5,"3-0001",1,3),
(6,"3-0002",1,3),
(7,"4-0001",1,4),
(8,"4-0002",1,4),
(9,"1-0003",2,1),
(10,"1-0004",2,1),
(11,"2-0003",2,2),
(12,"2-0004",2,2),
(13,"3-0003",2,3),
(14,"3-0004",2,3),
(15,"4-0003",2,4),
(16,"4-0004",2,4),
(17,"1-0005",3,1),
(18,"1-0006",3,1),
(19,"2-0005",3,2),
(20,"2-0006",3,2),
(21,"3-0005",3,3),
(22,"3-0006",3,3),
(23,"4-0005",3,4),
(24,"4-0006",3,4),
(25,"1-0007",4,1),
(26,"1-0008",4,1),
(27,"2-0007",4,2),
(28,"2-0008",4,2),
(29,"3-0007",4,3),
(30,"3-0008",4,3),
(31,"4-0007",4,4),
(32,"4-0008",4,4);
I'd like to get such result :
client_shipment_id good_name price count
1 desk 100 2
1 sofa 200 2
1 chair 300 2
1 bed 400 2
2 desk 100 2
2 sofa 200 2
2 chair 300 2
2 bed 400 2
3 desk 110 2
3 sofa 210 2
3 chair 310 2
3 bed 410 2
4 desk 110 2
4 sofa 210 2
4 chair 310 2
4 bed 410 2

Combine data from three unrelated tables

I need to use a data visualization tool that can only query a single source for a given chart. I have three tables with the data I need to visualize. So, I need to combine them into a single view or output table. Here are the table schemas:
MySQL [bdCaloriesNeeded]> desc activity;
+---------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | text | YES | | NULL | |
| Gender | text | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| length | text | YES | | NULL | |
| weight | int(11) | YES | | NULL | |
| exercise | int(11) | YES | | NULL | |
| food_consumed | int(11) | YES | | NULL | |
| date | datetime | YES | | NULL | |
+---------------+----------+------+-----+---------+-------+
MySQL [bdCaloriesNeeded]> desc exercise;
+---------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------+------+-----+---------+-------+
| Gender | text | YES | | NULL | |
| Min_Age | int(11) | YES | | NULL | |
| Max_Age | int(11) | YES | | NULL | |
| min_exercise_hours | int(11) | YES | | NULL | |
| med_exercise_hours | int(11) | YES | | NULL | |
| high_exercise_hours | int(11) | YES | | NULL | |
+---------------------+---------+------+-----+---------+-------+
MySQL [bdCaloriesNeeded]> desc food;
+---------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------+------+-----+---------+-------+
| size | text | YES | | NULL | |
| min_pounds | int(11) | YES | | NULL | |
| max_pounds | int(11) | YES | | NULL | |
| min_food_oz_per_day | int(11) | YES | | NULL | |
| max_food_oz_per_day | int(11) | YES | | NULL | |
+---------------------+---------+------+-----+---------+-------+
Here's the actual source data in the above tables:
MySQL [bdCaloriesNeeded]> select * from activity;
+------+----------+--------+------+--------+--------+----------+---------------+---------------------+
| id | name | Gender | age | length | weight | exercise | food_consumed | date |
+------+----------+--------+------+--------+--------+----------+---------------+---------------------+
| 14 | spot | M | 2 | 2'7" | 13 | 5 | 13 | 2017-10-08 00:00:00 |
| 67 | princess | F | 6 | 3'3" | 75 | 3 | 15 | 2017-09-05 00:00:00 |
+------+----------+--------+------+--------+--------+----------+---------------+---------------------+
MySQL [bdCaloriesNeeded]> select * from exercise
+--------+---------+---------+--------------------+--------------------+---------------------+
| Gender | Min_Age | Max_Age | min_exercise_hours | med_exercise_hours | high_exercise_hours |
+--------+---------+---------+--------------------+--------------------+---------------------+
| M | 1 | 2 | 1 | 4 | 6 |
| M | 3 | 7 | 1 | 3 | 4 |
| M | 8 | 15 | 1 | 2 | 2 |
| F | 1 | 2 | 1 | 4 | 6 |
| F | 3 | 7 | 1 | 3 | 5 |
| F | 8 | 15 | 1 | 2 | 2 |
+--------+---------+---------+--------------------+--------------------+---------------------+
MySQL [bdCaloriesNeeded]> select * from food;
+--------+------------+------------+---------------------+---------------------+
| size | min_pounds | max_pounds | min_food_oz_per_day | max_food_oz_per_day |
+--------+------------+------------+---------------------+---------------------+
| small | 1 | 10 | 12 | 18 |
| medium | 11 | 30 | 15 | 30 |
| large | 31 | 100 | 25 | 50 |
+--------+------------+------------+---------------------+---------------------+
Here's the SQL I'm executing:
SELECT activity.id, activity.name, activity.Gender, activity.age, activity.weight, activity.exercise, activity.date, exercise.min_exercise_hours, exercise.high_exercise_hours, food.size, food.min_food_oz_per_day, food.max_food_oz_per_day
from activity, exercise, food
where (
activity.exercise between exercise.min_exercise_hours and exercise.high_exercise_hours
)
and
(
activity.weight between food.min_pounds and food.max_pounds
)
and
(
activity.Gender = exercise.Gender
)
Here's the undesired result I'm getting:
+------+----------+--------+------+--------+----------+---------------------+--------------------+---------------------+--------+---------------------+---------------------+
| id | name | Gender | age | weight | exercise | date | min_exercise_hours | high_exercise_hours | size | min_food_oz_per_day | max_food_oz_per_day |
+------+----------+--------+------+--------+----------+---------------------+--------------------+---------------------+--------+---------------------+---------------------+
| 14 | spot | M | 2 | 13 | 5 | 2017-10-08 00:00:00 | 1 | 6 | medium | 15 | 30 |
| 67 | princess | F | 6 | 75 | 3 | 2017-09-05 00:00:00 | 1 | 6 | large | 25 | 50 |
| 67 | princess | F | 6 | 75 | 3 | 2017-09-05 00:00:00 | 1 | 5 | large | 25 | 50 |
+------+----------+--------+------+--------+----------+---------------------+--------------------+---------------------+--------+---------------------+---------------------+
I'm getting two rows for Princess. I need one row for each dog. The desired result should use Princess's's weight to look up the correct range of food per day, and use her gender and age to look up the correct range of exercise.
I've been banging on this for hours, can't see what doing wrong here.
So interestingly your question says that the tables are Unrelated but they are actually related and this is the whole point of a relational database, to join data based on those relationships.
The issue is that your exercise table is only being joined on exercise hours using the between so princess matches rows 4 and 5 in the exercise table. (the first where clause matches rows 1 and 2 also but the later where clause limits the Gender)
It looks to me like you should also limit the match on the exercise table to age as well as exercise and gender
so add
and (activity.age between exercise.min_age and exercise.max_age)
Also personally i like to use JOIN clauses rather than WHERE - it keeps all the stuff together.
SELECT activity.id,
activity.name,
activity.Gender,
activity.age,
activity.weight,
activity.exercise,
activity.date,
exercise.min_exercise_hours,
exercise.high_exercise_hours,
food.size,
food.min_food_oz_per_day,
food.max_food_oz_per_day
FROM activity
JOIN exercise
ON activity.exercise BETWEEN exercise.min_exercise_hours AND exercise.high_exercise_hours
AND activity.Gender = exercise.Gender
AND activity.age BETWEEN exercise.min_age AND exercise.max_age
JOIN food
ON activity.weight BETWEEN food.min_pounds AND food.max_pounds
Since you are looking for things that may be OUTSIDE of the ranges suggested you may want to consider LEFT JOIN on the exercise and food tables, so that the dogs on the activity table that fall outside of any range will still show up (with NULL values for the missing data for the other table.)
just change the join lines to LEFT JOIN like so:
LEFT JOIN exercise
LEFT JOIN food
See also: What is the difference between "INNER JOIN" and "OUTER JOIN"?

Selecting from two different tables and order by for one and limit results of the other?

Please someone help.
I have two tables, car_table and add_table, they are completely different tables.
car_table:
+---------+-------------+------+-----+------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+------------------+----------------+
| id | int(3) | NO | PRI | NULL | auto_increment |
| car | varchar(15) | NO | | NULL | |
| manuf | varchar(10) | NO | | NULL | |
|timestamp| timestamp | NO | | CURRENT_TIMESTAMP| |
+-------+-------------+------+-----+---------+---------------------------+
Sample Data:
+----+--------+-------+-----------+
| id | car | manuf | timestamp |
+----+--------+-------+-----------+
| 1 | M5 | BMW | time |
| 2 | Golf | VW | time |
| 3 | Toyota |pickup | time |
| 4 | Ford | focus | time |
+----+--------+-------+-----------+
add_table
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
|add_id | int(3) | NO | PRI | NULL | auto_increment |
| link | varchar(15) | YES | | NULL | |
| s_img | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
Sample Data:
+-------+--------+-------+
|add_id | link | s_img |
+-------+--------+-------+
| 1 | link | img |
| 2 | link | img |
| 3 | link | img |
| 4 | link | img |
+-------+--------+-------+
I am trying to write a select statement that will order the car_table by timestamp and limit the add_table to 3 results, mixing the results so that a add_table results are separated by car_table results.
Below is what I have so far but not sure where to go from here. (update: this seems to partially work, however not sure what it is ordering the add_table by and not sure if it's even the correct way of doing it.)
SELECT id,car,manuf,timestamp,
FROM car_table
UNION ALL
SELECT add_id,link,s_img,NULL AS timestamp
FROM add_table ORDER BY TimeStamp

MySQL query not producing the correct result

Given this:
MariaDB [master]> describe history;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| historyid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| amount | float | NO | | NULL | |
| subsidy | char(1) | NO | | NULL | |
| last_payment | date | NO | | NULL | |
| amount_paid | float | NO | | NULL | |
| balance | float | NO | | NULL | |
| attend | char(1) | NO | | N | |
| attend_date | date | NO | | NULL | |
| groupid | int(11) unsigned | NO | | NULL | |
| clientid | int(10) unsigned | NO | MUL | NULL | |
| memberid | int(10) unsigned | NO | MUL | NULL | |
+--------------+------------------+------+-----+---------+----------------+
MariaDB [master]> describe participation;
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| partid | int(11) | NO | PRI | NULL | auto_increment |
| notes | varchar(255) | NO | | NULL | |
| groupdate | date | NO | | NULL | |
| clientid | int(10) unsigned | NO | MUL | NULL | |
| memberid | int(10) unsigned | NO | MUL | NULL | |
+-----------+------------------+------+-----+---------+----------------+
MariaDB [master]> select * from participation;
+--------+-----------+------------+----------+----------+
| partid | notes | groupdate | clientid | memberid |
+--------+-----------+------------+----------+----------+
| 194 | test test | 2016-01-26 | 3 | 1 |
+--------+-----------+------------+----------+----------+
How do I write the following query
MariaDB [master]> SELECT attend_date, groupdate, h.clientid, h.memberid
-> FROM history AS h
-> LEFT JOIN participation AS p ON groupdate = attend_date
-> WHERE h.memberid = "1"
-> AND MONTH(attend_date) = "1"
-> AND YEAR(attend_date) = "2016"
-> AND attend_date <> "0000-00-00"
-> ORDER BY attend_date ASC;
+-------------+------------+----------+----------+
| attend_date | groupdate | clientid | memberid |
+-------------+------------+----------+----------+
| 2016-01-26 | 2016-01-26 | 3 | 1 |
| 2016-01-26 | 2016-01-26 | 4 | 1 |
| 2016-01-26 | 2016-01-26 | 1 | 1 |
| 2016-01-26 | 2016-01-26 | 2 | 1 |
| 2016-01-28 | NULL | 3 | 1 |
| 2016-01-28 | NULL | 4 | 1 |
| 2016-01-28 | NULL | 1 | 1 |
| 2016-01-28 | NULL | 2 | 1 |
+-------------+------------+----------+----------+
so that my return is like this
+-------------+------------+----------+----------+
| attend_date | groupdate | clientid | memberid |
+-------------+------------+----------+----------+
| 2016-01-26 | 2016-01-26 | 3 | 1 |
| 2016-01-26 | NULL | 4 | 1 |
| 2016-01-26 | NULL | 1 | 1 |
| 2016-01-26 | NULL | 2 | 1 |
| 2016-01-28 | NULL | 3 | 1 |
| 2016-01-28 | NULL | 4 | 1 |
| 2016-01-28 | NULL | 1 | 1 |
| 2016-01-28 | NULL | 2 | 1 |
+-------------+------------+----------+----------+
The whole idea here is to be able to pull all the clients who don't yet have notes applied to a groupdate matching history table's attend_date. And only when a note is posted will the groupdate be set to the attend_date. Yet, the query, as I have it , returns a groupdate of 2016-01-26 for all four clients instead of just the one. Thanks in advance for your help.
Looking at your table structure, I see the relationship between your attend_date and groupdate on which you attempted to join, but I also see that those tables have clientid and memberid in common.
Since you only joined on the date, you're getting back a cartesian product of all clients and all dates for that condition. You can make the join's ON clause more specific by adding additional conditions with a logical AND.
LEFT JOIN participation AS p
ON groupdate = attend_date
AND h.clientid = p.clientid
It isn't fully obvious to me if your desired result set also requires a condition to match memberid between those tables, but if it does, the solution is as simple as another AND.
LEFT JOIN participation AS p
ON groupdate = attend_date
AND h.memberid = p.memberid
It helps not to think of a join's ON clause as merely a match between common columns, though that's the most common way it is used. Instead the ON clause just needs to present some condition that when true results in a joined row being returned (just like you're used to with a WHERE clause). That means you can place just about anything there which can be evaluated as true or false, allowing for complex joining logic. And the same can be said for ORDER BY and GROUP BY.

MySQL query from multiple databases and select random

I would like to select random entries from a single table based on information coming from two other tables (one saved in a different database). The tables are as follows:
1- In databaseA called "islands" contains:
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| chrom | int(11) | NO | | NULL | |
| start | int(11) | NO | | NULL | |
| end | int(11) | NO | | NULL |
the indexes are:
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| islands | 0 | PRIMARY | 1 | id | A | 15991 | NULL | NULL | | BTREE | | |
| islands | 1 | locations | 1 | line_string | A | NULL | 32 | NULL | | SPATIAL | | |
To select from this database I normally use:
SELECT * FROM islands FORCE INDEX (locations)
WHERE MBRIntersects(GeomFromText('Linestring(1 120, 1 120)'), line_string)
2 - In databaseB call "Context" contains:
+---------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| chrom | int(11) | NO | | NULL | |
| site | int(11) | NO | | NULL | |
| context | char(3) | NO | | NULL | |
There are only 4 possible contexts in this table (which are indexed)
3 - In databaseB called "Entries" Contains:
+-------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| chrom | int(11) | NO | MUL | NULL | |
| site | int(11) | NO | MUL | NULL | |
| methylation | float | NO | | NULL | |
I would like to select a random entry from the entries table that is located in the context table with a context entry of "CpG", can be located in the "islands" table or not (I have two different searches).
Update:
Based on comments below I am using
SELECT * FROM
(SELECT t.chrom, t.site, t.methylation, c.context
FROM f1 as t INNER JOIN context as c on c.chrom = t.chrom AND c.site = t.site
WHERE c.context = 'CpG'
) AS s LIMIT 2;
To get:
+-------+-----------+-------------+---------+
| chrom | site | methylation | context |
+-------+-----------+-------------+---------+
| 1 | 10003735 | 69 | CpG |
| 1 | 100063074 | 98.79 | CpG |
+-------+-----------+-------------+---------+
I would like to now join these results to the islands table to get the sites from the first part that are found within island regions (or not). I am using:
SELECT * FROM
(SELECT t.chrom, t.site, t.methylation, c.context
FROM f1 as t INNER JOIN context as c on c.chrom = t.chrom AND c.site = t.site
WHERE c.context = 'CpG'
) AS s
CROSS JOIN islands as i
WHERE MBRINTERSECTS(GeomFromText('Linestring(s.chrom s.site, s.chrom s.site)'), i.Line_string)
LIMIT 2;
However, this gives me an empty set (it shouldn't).