0

I'm very new to this - could someone let me know if this query is correct? I'm trying to take a variable from table 1 and use it to count lines in table 2... Many thanks.

INSTRUCTION Take the following hypothetical 2 table database: Table 1, called content, has the fields: content_id, content_name, content_url. Table 2, called views, has the following fields: view_id, content_id, view_timestamp

Write a MySQL query to find the total number of views for a piece of content with the content_name "maria_video".

MY QUERY mysql> SELECT @ID:=content_id FROM content WHERE content_name=”maria_video”;

mysql> COUNT(*) FROM views WHERE content_id=@ID;

1 Answers1

0

Based on this

Write a MySQL query to find the total number of views for a piece of content with the content_name "maria_video".

Couldn't you just write a single query that gets the same value?

Something like this

SELECT COUNT(*) FROM Views AS v INNER JOIN Content AS c ON (v.content_id=c.content_id) WHERE c.content_name="maria_video"

I don't have a mysql instance available to test it right now, but it should work.

Carlos Grappa
  • 2,351
  • 15
  • 18