Music Database Site:
I have a table named ps_albums with columns: artist, album, genre_id, and so on.
I also have a second table named ps_users with the following column: user_id, date_joined, fave_genre_1, fave_genre_2, fav_genre_3 and other.
My overall goal is to display only the genres of music that a user selects for the top 3 favorite selections.
I am able to INSERT the genre_id of all 3 favorite genre selections into ps_profiles which hold this info. Now I need to be able to pull the 3 genres_id's and display only them instead off all genres by default.
So by default SELECT * FROM ps_albums ORDER by DESC;
Thus displaying ALL albums on the front page.
Now, when that user clicks 'My Favorite Genres' I do this...
$query= "SELECT * FROM `ps_profiles` WHERE `user_id`= $user_id";
$row = #mysql_fetch_object(#mysql_query($query));
$genre1 = $row->fav_genre_1;
$genre2 = $row->fav_genre_2;
$genre3 = $row->fav_genre_3;
I want to be able to display all records from ps_albums according to the 3 favorite selections from ps_profiles.
How would I setup the select statement?
SELECT * FROM ps_albums WHERE genre=$genre1 AND genre=$genre2 AND genre=$genre3
How would I go about this? There are more then 10 genres but I only want to show the ones selected as favorites from the 3 columns. Hopes this clarifys a bit more.
SELECT * FROM ps_albums WHERE genre in ($genre1,$genre2,$genre3)
You could use either OR's or IN() to match a column value with multiple possible value.
For example:
SELECT * FROM t WHERE a=1 OR a=2 OR a=3;
SELECT * FROM t WHERE a IN(1,2,3);
So for your case, it could look something like this:
$query = "SELECT * FROM ps_albums WHERE genre IN('$genre1', '$genre2', '$genre3')";
I added quotes in the query, I'm assuming genre's are strings?
Related
I edited the post for the real problem
I tried to make a movie website.
I have a movie table
id, moviename, actorsid
1, movie1, 1|2|3
2, movie2, 1|5|4
And i have a actors table
id, actorname
1, actor1
2, actor2
3, actor3
I want to display all actors movies, so i made a SELECT query in movie table, with the actor id that it had
SELECT * FROM `movies` WHERE actors_id IN('4')
run successfully if the movie has i actor id only
SELECT * FROM `movies` WHERE actors_id LIKE '%4%'
run successfully if i don't have any other id contain 4.. like 41 or 14 or 214
Option 1:
you can try something like below:
SELECT * FROM `TB` WHERE
usersid REGEXP '^31\\|' OR
usersid REGEXP '\\|31$' OR
usersid REGEXP '\\|31\\|'
Fiddle: http://sqlfiddle.com/#!9/b88de5/22
Please test all scenario before you implement.
Option 2: You can use FIND_IN_SET after replacing pipe with comma like below:
SELECT * FROM `TB` WHERE FIND_IN_SET("31",replace(usersid,"|",","))
Fiddle: http://sqlfiddle.com/#!9/b88de5/6
I am new to PHP and MySQL.
I wanted to know that if its possible to show all the rows from the table but first where id='some value'. ie.
SELECT * FROM users ORDER BY id='some value'
Thanks for answering.
You can make a set of queries, for example:
SELECT * FROM users WHERE id= 5 UNION SELECT * FROM users WHERE id != 5;
Or if you need something like this example:
SELECT * FROM users WHERE id >= 5;
I Need to select a specific row from a MySQLi database based off of a value in it.
For example I have a Table with a column named "house", and under that column there are maybe 5 rows with the title "house1" and three rows with the title "house2". I only want to select the rows that have "house1" in them.
This is my code
$query = "SELECT * FROM Hockey WHERE house = house1 ORDER BY attendance desc";
I then want it to make a table with only values from a row if the house is Jacksons
right now if I delete the WHERE part from my query it will make a table but it will have rows from both houses (Jacksons and Martlands)
Thanks!
$query = "SELECT * FROM Hockey WHERE house = 'house1' ORDER BY attendance desc";
I've seen that question several times but I can't find out how to display the result. I have a movie database and I would like the count of each genre of movie in my top menu in a single MySQL query so my menu would display like this:
Total Movies (300)
Drama (50)
Comedy (75)
Thriller (30)
...and so on...
I've found some MySQL query on this site but no one specify HOW to handle the counts after the SQL query. Something like this would probably work:
select genre, count(*) from movies group by genre
But how do I display the count for each value afterwards?
Thank you very much!
Alias the count part so you have an easily accessible column name:
SELECT genre, count(*) AS nb_movies FROM movies GROUP BY genre
then you can access it like $row['nb_movies'].
Without the alias the aggregate column takes the name of the aggregate function call which produced it, so in your case it would be accessed like $row['count(*)'].
Try
select genre, count(*) AS total from movies group by genre
Use total as your count for eg.
echo $result['total'];
Try this,
SELECT genre, count(*) AS total_genre_movies FROM movies GROUP BY genre
Now you can access like $result['total_genre_movies']
It's easier if you alias the result of count(*)
select genre,
count(*) as total
from movies
group by genre
Then you can access it as $row['total'] when you fetch the result into $row in exactly the same way you'd reference $row['genre']
select genre, count(*) as genre_count from movies group by genre
Now you can access like $result['genre_count']
Try this,
$result = mysql_query("select genre, count(*) as genre_count from movies group by genre");
while($row = mysql_fetch_array($result)) {
$genre = $row['genre'];
$genre_count = $row['genre_count'];
}
I want to select one same column from 3 tables and get a rows from this one column.
Here is the code:
mysql_query("SELECT * from tv,movies WHERE hash='123'");
So now i want the column called hash from the tv and movies to bring result from the hash number.
I don't have the the same columns number in tv and movies.
Make sure if the hash doesn't exist in tv then go to movies.
Make it one table with category field in it.
that's the only proper way of designing a database.
SELECT fieldName1 FROM table1 WHERE hash='123'
UNION
SELECT fieldName1 FROM table2 WHERE hash='123'
UNION
SELECT fieldName1 FROM table3 WHERE hash='123';
You have to select equal number of columns in both tables
SELECT tvcol1 as field1,tvcol2 as field2 from tv WHERE hash='123'
UNION
SELECT moviescol1 as field1,moviescol2 as field2 from movies WHERE hash='123'
Reading between the lines and based on your comments to #Bryan's answer, you want all the columns from the tv table, but if it's not found select all the columns from the movies table. In that case use two different queries:
$result = mysql_query("SELECT * FROM tv WHERE hash = '123'") or die( mysql_error() );
if( mysql_num_rows( $result ) == 0 ) {
$result = mysql_query("SELECT * FROM movies WHERE hash = '123'") or die( mysql_error() );
}
Now $result has the columns from either tv or movies table, or false if the hash wasn't found in either of them.