Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL query with three different tables with distinct
    text
    copied!<p>I'm having some trouble with a SQL query across 3 tables with different attributes. Here are the tables and the attributes that I'd like to query in each of them:</p> <blockquote> <p>news_stories - time, headline</p> <p>per_minute_quotes - security_id, timestamp, last_price</p> <p>securities - name, id_bb, id</p> </blockquote> <p>What I'd like to do is retrieve a security name, id from the securities table, find headlines that correspond to that security (with a timestamp) from the *news_stories* table and find the last_price for that security at the same time as the article from the per_minute_quotes table. </p> <p>Does this make sense? Please see what I've managed to do so far below... </p> <pre><code>SELECT DISTINCT `news_stories`.`time` , `securities`.`name` , `adjusted_daily_quotes`.`security_id` , `news_stories`.`headline` , `securities`.`id_bb` , `securities`.`id` FROM `schema`.`adjusted_daily_quotes` , `schema`.`securities` , `schema`.`news_stories` WHERE ( (`adjusted_daily_quotes`.`security_id`) = '498' AND (`securities`.`id`) = '498' AND (`securities`.`id_bb`) LIKE '267%' AND (`news_stories`.`headline`) LIKE '%:267') LIMIT 0,50; </code></pre> <p>This will basically do the first part of my query, ie. it isn't connected with the last_price. Here is my attempt at doing that:</p> <pre><code>SELECT DISTINCT `news_stories`.`time` , `securities`.`name` , `per_minute_quotes`.`security_id` , `news_stories`.`headline` , `securities`.`id_bb` , `securities`.`id` , `per_minute_quotes`.`timestamp` , `per_minute_quotes`.`last_price` FROM `schema`.`per_minute_quotes` , `schema`.`securities` , `schema`.`news_stories` WHERE ( (`per_minute_quotes`.`security_id`) = '498' AND (`securities`.`id`) = '498' AND (`securities`.`id_bb`) LIKE '267%' AND (`news_stories`.`headline`) LIKE '%:267 HK' AND (`per_minute_quotes`.`timestamp`) &lt;= (`news_stories`.`time`)) LIMIT 0,5; </code></pre> <p>However, this query returns 5 of the same headline for some reason, all with the same time. I would really appreciate help with forming this query. Does that have something to do with the DISTINCT operator? I've tried using GROUP BY but with no luck.</p> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload