James
James

Reputation: 43617

MYSQL get column

There is a table movie_meta with columns meta_id, movie_id, meta_key, meta_value

I know value of the movie_id and want to get value of meta_value of the meta_key "links".

Like we have a row 1|24|links|http://google.com inside "movie_meta"

We make a request for movie_id = 24, and get $link = 'http://google.com';

What is a true SELECT for this?

Tryed this, but it gives all the columns:

("SELECT * FROM movies WHERE movie_id = 24 AND meta_key = links"")

Thanks.

Upvotes: 1

Views: 441

Answers (3)

Joe Stefanelli
Joe Stefanelli

Reputation: 135729

SELECT meta_value AS link
    FROM movies 
    WHERE movie_id = 24 
        AND meta_key = 'links'

Upvotes: 2

OMG Ponies
OMG Ponies

Reputation: 332521

Use:

SELECT m.meta_value 
  FROM MOVIES m 
 WHERE m.movie_id = ? 
   AND m.meta_key = 'links'
  • Strings need to be enclosed in single quotes to be interpreted as such in SQL.
  • SELECT * returns all columns from the table

PHPified, using sprintf:

$query = sprintf("SELECT m.meta_value 
                    FROM MOVIES m 
                   WHERE m.movie_id = %d 
                     AND m.meta_key = '%s'", 
                  $movie_id,
                  $meta_value)

Upvotes: 2

codaddict
codaddict

Reputation: 454950

SELECT * will return all columns.

To get only the value of meta_value use select meta_value ...

Full query:

SELECT meta_value
FROM movie_meta
WHERE movie_id = 24 AND meta_key = '$link'

Upvotes: 3

Related Questions