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
Related
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' am storing the profile friends ID in a string format comma delimited. When the run the Query, its gives 1 record whereas it should return with 2 records.
MySQL Query
SELECT * FROM PROFILES WHERE profile_id IN(SELECT profile_friends FROM PROFILES WHERE profile_id = '1')
Which gives this result (The results should be 2 records NOT 1 Record)
When I run the following Query, gives me two ID's that are in a profile_friends field.
SELECT profile_friends FROM PROFILES WHERE profile_id = '1'
Please use following Query
SELECT * FROM PROFILES WHERE FIND_IN_SET(profile_id,(SELECT profile_friends FROM PROFILES WHERE profile_id = '1'))
IN clause only use when you search value from integer field
But your field(profile_friends) is string so following clause you need to use.
FIND_IN_SET
i have two tables in my db .. one is Messages and the other is Contacts... both tables contain the mobile_number field.. in Contacts table there is no duplicate numbers ... but in Messages tables there are several duplicate numbers in mobileNo field...what i am doing is write now i am selecting the distinct mobile numbers from the Messages tables and then i am comparring the distinct numbers from the contacts table ... so if the messages_mobileNo is found on the contacts table then give me the contact name against the number otherwise messages_mobileNo ... so the problem is distinct not working .. i am not able to get the distinct numbers from the Messages table ... it is showing me the duplicate numbers
here is my query
SELECT DISTINCT Message.mobileNo,
Contact.mobileNo,
Contact.workNo,
Contact.homeNo,
Contact.other,
Contact.name,
Message.body,
Message.idTextMessage
FROM cakephp_db.textmessage AS Message
LEFT JOIN cakephp_db.contacts AS Contact ON (Message.user_id = Contact.user_id
AND ((Message.mobileNo = Contact.mobileNo)
OR (Message.mobileNo = Contact.workNo)
OR (Message.mobileNo = Contact.homeNo)
OR (Message.mobileNo = Contact.other)))
WHERE Message.User_id = 23
ORDER BY Message.idTextMessage DESC LIMIT 6
So your trying to get the last 6 messages of a person if I'm right?
SELECT Message.mobileNo,
Contact.mobileNo,
Contact.workNo,
Contact.homeNo,
Contact.other,
Contact.name,
Message.body,
Message.idTextMessage
FROM cakephp_db.textmessage AS Message
LEFT JOIN cakephp_db.contacts AS Contact ON Message.user_id = Contact.user_id
AND Message.mobileNo IN (Contact.mobileNo, Contact.workNo, Contact.homeNo, Contact.other)
WHERE Message.User_id = 23
GROUP BY Message.mobileNo
ORDER BY Message.idTextMessage DESC LIMIT 6
add a GROUP BY Message.mobileNo before your ORDER BY
If you are using MySQL SELECT DISTINCT with an ORDER BY added to it, you will have to create a temporary table where it can store its results. Here is a link offering more help:
MySQL DISTINCT Optimization
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.
I have a mysql query:
$query = "SELECT * FROM movielist WHERE genre = 'comedy' ORDER BY dateadded DESC";
I need to change it so that it selects from movielist where genre contains 'comedy'.
(Note: that my genre field will often contain more than one genre example "comedy, action, drama".
You need to LIKE the genre:
$query = "SELECT * FROM movielist WHERE genre LIKE '%comedy%' ORDER BY dateadded DESC"
$query = "SELECT * FROM movielist WHERE genre LIKE '%comedy%' ORDER BY dateadded DESC";
OR
$query = "SELECT * FROM movielist WHERE FIND_IN_SET('comedy', genre) ORDER BY dateadded DESC";
Second one is better in most cases.
Even better, you should use a separate table for facilitating many-to-many relationships.
i.e. New table called 'movies_genres' with two fields - movie_id and genre_id (both indexed foreign keys). Every time you add a new genre to a movie or a new movie to a genre, add an entry to this table.
To find all movies belonging to a particular genre:
SELECT movies.*
FROM movies
JOIN movies_genres
ON movies_genres.movie_id = movies.id
JOIN genres
ON movies_genres.genre_id = genres.id
WHERE genres.name = 'comedy'
To find all genres belonging to a particular movie:
SELECT genres.*
FROM genres
JOIN movies_genres
ON movies_genres.genre_id = genres.id
JOIN topics
ON movies_genres.movie_id = movies.id
WHERE movies.name = 'Citizen Kane'
With your schema you might be interested in find_in_set()
SELECT
x,y,z
FROM
movielist
WHERE
find_in_list('comedy', genre)
Keep in mind that this is not index-friendly and potentially slow.
With normalized tables you wouldn't have genre='comedy, action, drama' (i.e. structured data within one field) but a table
genres (
id int auto_increment,
genre_name varchar(...)
...
)
a table
movielist (
id int auto_increment,
title varchar(...)
...
)
and a table
moviegenres (
movieid int,
genreid int,
...
)
And a query that "glues" the information stored in these three tables together using one or two JOINs.
For an immediate solution, do a Dexter suggested. But in the long run you will get a lot of benefit from normalizing your database