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';
开发者_StackOverflow中文版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.
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'
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)
SELECT meta_value AS link
FROM movies
WHERE movie_id = 24
AND meta_key = 'links'
精彩评论