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.
Related
I have a php page that is submitted a search string which is separated by commas. I have to search the database to check if the search terms are first names or last names, as well as if they match any of the interests/skills/hobbies in my interest/skill/hobby tables.
After I explode the string by ", " I first make the query to search for names which is
SELECT *
FROM Users
WHERE FirstName = 'term1'
OR FirstName = 'term2'
OR LastName = 'term1'
OR LastName = 'term2'.
Then the query for searching by interests skills and hobbies and the users that have those tags is this
SELECT user_id from (
(SELECT *
from UserInterests
WHERE interest_id = interestid1
OR interest_id = interestid2
)
UNION ALL
(SELECT *
from UserSkills
WHERE skill_id = skillid1
OR skill_id = skillid2
)
UNION ALL
(SELECT *
from UserHobbies
WHERE hobby_id = hobbyid1
OR hobby_id = hobbyid2
)
) a
GROUP BY user_id
ORDER BY count(*) DESC
What I want to do is be able to search something like John, Rockclimbing, Dancing. Then the user who matches the most of those terms would be shown first.
I tried combining the two in many different ways some of which don't even error, but I don't get any results in my ui.
UserInterests/UserSkills/UserHobbies are just two column tables with user_id and interest/skill/hobby_id
I have two tables, one for registered users and one to store votes.
We are logging in with registrants.id and registrants.zipcode. Once they vote their votes are inserted into the votes table, along with their Registration ID.
Im trying to right a select statement that returns a record that will select all the records for Matched ID and Zipcode, but the ID is not in the Votes.voter column. i have tried all kinds of variations of all the joins i can think of. is it something simple i am missing.
SELECT * FROM registrants
LEFT JOIN votes on registrants.id = votes.voter
WHERE registrants.id = 1 AND registrants.zipcode = 46706 and votes.voter <> 1
Perhaps a not exists query:
select * from registrants
where registrants.zipcode = '46706'
and not exists (select 1 from votes where registrants.id = votes.voter)
I have two tables that have an id field with the same name. I didn't think I'd ever need to mix the two but there's one page where I need to. I can't join the tables because they both have completely separate data and no fields in common.
I can union them but the ID field is the same name and many identical numbers (which do not relate). I can't change the name in the tables but I need the field names to be different when put into a variable (using PHP).
I tried something like this:
SELECT date, id as id1
FROM football
UNION
SELECT date, id as id2
FROM basketball
ORDER BY date
But that just gives me one field (id1). I need the result to be in such a way that I can do this:
foreach ($rows as $row) {
if (!empty($row['id1'])) {
$id = $row['id1'];
$sport = "football";
} else {
$id = $row['id2'];
$sport = "basketball";
}
echo "my number is $id and I play $sport";
}
From MySQL Union Syntax
The column names from the first SELECT statement are used as the
column names for the results returned.
You could assign sport in your query:
SELECT date, id, 'football' as sport
FROM football
UNION
SELECT date, id, 'basketball' as sport
FROM basketball
ORDER BY date
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?
I have two tables. First table is je_addchoice, which contains fields like
choiceid
pollid
choicename
choicecreatorid
and the second table is je_uservote and the fields are
userid
pollid
choiceid
What i want to do is,
Display the choice names based on the no of votes in the je_uservote table
$query = select * from je_addchoice where poll_id='$poll_id' //order by (count(choiceid)) from second table
//QUERY FOR DISPLAY CHOICENAMES BASED ON COUNT OF VOTES
How to write the above query
My question is how to access the no of counts in the jeuservote table and display the choicenames based on the result count. Actually the votes for the choicenames in the addchoice table count is stored in the jeuservote table. How can i access the vote count for the choice names
SELECT *, (
SELECT count(*)
FROM je_uservote T2
WHERE T2.pollid=T1.pollID
AND T2.choiceid=T1.choiceID) AS votes
FROM je_addchoice T1
ORDER BY votes