0

I have a sql query that is working well but I need a way to extend this query to also get information from another table called LineUps.

Original query:

$stmt = $conn->prepare("SELECT channel, description, Tier
FROM Channel_LineUps WHERE Market_ID = 1 ORDER BY Tier ASC"); 

I need to also now get data from a different table called.

Columns are called DIG and HD in the LineUps table.

I tried the following but does not work:

$stmt = $conn->prepare("SELECT Channel_LineUps.channel,   Channel_LineUps.description, Channel_LineUps.Tier, LineUps.HD, LineUps.DIG FROM Channel_LineUps, LineUps WHERE Channel_LineUps.Market_ID = 1 ORDER BY Channel_LineUps.Tier ASC"); 

i want to be able to determine where a channel is marked as yes for DIG or HD and so thinking I'll need a single query for this.

Any ideas?

2
  • 2
    Please provide table structure and sample data. At first sight, it looks like you have no join condition so you get a Cartesian join which probably takes too long to render. Commented Feb 9, 2015 at 20:40
  • what key do the lineups and channel_lineups tables have in common? Commented Feb 9, 2015 at 20:43

1 Answer 1

1

You need to create a join between two tables that is you need have a common column from first table (Channel_lineUps) to LineUps table and then you can have a inner join on these two table like channel_LineUps.id=LineUps.related_column

you can in your where clause just add like where Channel_lineUps.market_id=LineUps.market_id and it should return the related data.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok thanks. Market_ID is common to both. How would I put this in the php code?
Tried the following but receive an error: $stmt = $conn->prepare("SELECT Channel_LineUps.channel, Channel_LineUps.description, Channel_LineUps.Tier, LineUps.HD, LineUps.DIG FROM Channel_LineUps, LineUps WHERE Channel_LineUps.Market_ID=LineUps.Market_ID AND Market_ID = 1 ORDER BY Channel_LineUps.Tier ASC");
Again, it would be easier if you provide table structure and sample data
Not sure if i know that. Not a sql guy. I think, my issue is somehow related to doing a where on Market_ID =1 - I need to limit to that Market_ID but if I insert the where clause to join "WHERE Channel_LineUps.Market_ID=LineUps.Market_ID" then it seems i do not have a way to limit to Market_ID = 1 The query seems to work but as i say, is not limiting the market by Market_ID
i ended up using this query which seems to solve the issue. I don;t think I provided you all with enough info to solve this. $stmt = $conn->prepare("SELECT distinct Channel_LineUps.channel, Channel_LineUps.description, Channel_LineUps.Tier, LineUps.HD, LineUps.DIG FROM Channel_LineUps, LineUps WHERE (LineUps.Market_ID = Channel_LineUps.Market_ID AND Channel_LineUps.Market_ID = 1) ORDER BY Channel_LineUps.Tier ASC");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.