First of all, I'd just want to say that I'm sorry for the poor title. I'm really struggling with explaining the problem I'm facing in just a short sentence.
I have a table called actors which contains an aID (primary key) and an aName and some more stuff. I also have a table called videos which contains vID (primary key) and other data. The third and last table I have is called connections and that one contains a primary key, a cVideoID and a cActorID.
Let's say I have created a video and when I navigate to the video *www.example.com/video.php?v=primary_key* I'd like to print out all actors who are in that movie (actor1, actor2, actor3 and actor4). Therefor I created the connections-table to keep track of all movie-actor connections. For every movie I create I connect the actors with the movie.
I thought that I could do something like this:
<?php
$result2 = mysql_query("SELECT `actors`.`aID`, `actors`.`aName` FROM `actors` WHERE `connections`.`cVideoID` = {$_get['v']}");
while($actors = mysql_fetch_array($result2))
{
echo "<a href='actor.php?id={$actors['aID']}>{$actors['aName']}</a> ';
}
?>
But it seems like that's not working. Any ideas?
What you want to do, is select all of the entries from connections, where the video ID is the selected video. Then, you JOIN on the actors table, to get all of the information about the actors that were found.
Example: Get all of the actor's names for a specific video ID:
SELECT a.aName
FROM connections c
LEFT JOIN actors a
ON a.aID = c.aID
WHERE c.vID = 1;
SQL Fiddle
What you need here is a join.
A normal left join works like this:
LEFT JOIN [name of table] [name of table you will want to use]
ON ([where statements searching for the right columns to join])
So your query will look something like this:
SELECT a.aID, a.aName
FROM `connections` c
LEFT JOIN `author` a ON (c.cActorID=a.aID)
WHERE c.cVideoID=[id of the video]
Now first of all you say the database you want to catch the columns aID and aName from the table a next you use the FROM statement to "import" the connections table as "c". You then load the author table and make it accessible as "a" (see the select statement a.[...]) and you also say that it should join the two tables ON every c.cActorID=a.aID and in the end you make a where statement to declare you are only searching for videos with the c.cVideoId=[id]
Related
As you can see from the image I have a table called wpw8_postmeta where it has four columns and I have searched for the term release and got the following results.
I want to copy the results (release_date [meta_value]) to another table called wpw8_test using the post_id as foreign key/primary key.
The second table looks like this
After the update will be done it should look like this
You seem to be looking for the update ... join syntax:
update wpw8_test t
inner join wpw8_postmeta p on p.post_id = t.post_id
set t.release_date = p.meta_value
where p.meta_key like '%release%'
I have a textbox (for which I used jQuery auto-completion) in my PHP form where user can insert an actor/actress name and then by clicking "Search" button, a new window is opened showing list of movies by that actor/actress.
This is a query where I get movies by actor name:
$query = $conn->prepare("SELECT DISTINCT c.movieName, c.castName, c.movieImdbId, f.year, f.posterLink FROM cast_movie as c JOIN film_info as f ON c.ImdbId = f.ImdbId WHERE c.castName = :q");
$query->execute(array(':q' => $searchText ));
My question:
The above query works fine if user select a name from auto-completion list. However I would like to enable user to write any name (even if he couldn't find from auto-completion list). For example, If user write "tom" in the textbox, and click on "search" button, I want to show list of all movies by all actors that their name containing word "tom".
For this purpose, I used LIKE :q and ':q' => '%' . $searchText . '%' in the above query, but the query never ends!!(I think because cast_movie is a VIEW which is quite large (with 3 million rows) and joining this view with the other table takes a very long time (actually I waited for 10 minutes and it didn't finished yet).
Could someone kindly let me know if there is any way to fix this? (I read we can use index for joining very large tables, but I think it's not possible to define index for cast_movie since it is VIEW.)
UPDATE:
Regarding comments, in order to provide more information:
cast_movie is a view which is made by joining "movie_roleNames" and "movies".
movie_roleNames is also a view which is made by joining two tables "Cast" and "nameRoles".
film_info is also a view that is made by joining two tables "movies" and "movies_info".
The structure of the above tables:
table "movies": Id, movieName, ImdbId(unique Id of movies), Rate, numVotes, year (indexes: ImdbId, movieName, year)
table "cast": castName, castImdbID (unique Id of casts) (indexes: castName, castImdbID)
table "nameRoles": Id, castImdbId, movieImdbId, role_Id, (indexes: movieImdbId, castImdbId)
VIEW "movie_roleNames": Id, castName, castImdbId, movieImdbId and the join statement was: SELECT n.Id, c.castName, n.castImdbId, n.movieImdbId
FROM nameRoles as n
join Cast as c
ON n.castImdbId = c.castImdbID
VIEW "cast_movie": Id, castName, castImdbId, movieImdbId, movieName and the join statement was: SELECT m.Id, r.castName, r.castImdbId, r.movieImdbId, m.movieName
FROM movie_roleNames AS r
JOIN movies AS m
ON r.movieImdbId = m.ImdbId
All ideas are highly appreciated,
I'm having trouble with a join query, my issue is as follows.
Table: battles
Fields: id,attacker_id,defender_id
Table: users
Fields: id,profile_image
I would like to do a query to retrieve a battle and get the profile images as well from the other table.
Is there a way to do this in a single or do I have to do more than one?
Thanks in advance.
I wanted to wait a while to see if you had any attempt or if you will answer my first question to know if I understood the problem. But maybe you don't have a starting point. Try something like:
SELECT
a.profile_image as attacker_profile_image,
d.profile_image as defender_profile_image
FROM
`battles` b
LEFT JOIN
`users` a
ON
b.`attacker_id` = a.`id`
LEFT JOIN
`users` d
ON
b.`defender_id` = d.`id`
the problem here is the fact that you need to join with the users table twice, so you will need to create aliases for the columns you plan to use
This query will fetch the two images only, you will need to add the extra fields
Intro
Hello! I am in the process of building a lyrics website where I will store the lyrics in a MySQL database.
I want to have 2 main tables: 1 for the lyrics which will have id, lyrics title, lyrics text, artist name,and number of views.
The other table will be the artist table with: id, artist name
QUESTIONS:
How can I link the two tables by using the artist name field in both table? I want to display all the artists on my site from the table and see all the lyrics related to that particular artist/
How can I link to a particular record or field in a table?
Please help or if you know of any sites or videos that can help me learn these and other things that may help me in building my site would be appreciated.
Do not store the artist name in both tables. That defeats the purpose of having a separate artist table. Instead store the ArtistID in the Lyrics table, and only store the ArtistName in the Artist table. That way if you, for example, misspell an artist's name, you will only have to update one table, and you will not break the relationship between Lyrics and Artist.
Tables are linked together using Foreign Key relations.
I answer first question.
You should really change your lyrics table using an id_artist field instead of artist name.
This makes sure you don't duplicate names (so wasting space) and you can't write a wrong name leading you to wrong result during queries.
So you can do
SELECT a.`artist name`, l.title, l.text
FROM artist a INNER JOIN lyrics l
ON a.id = l.id_artist
WHERE a.name = '....'
// or you can use WHERE a.id = ...
I have been doing this for a while now, via some php, first lets say we have two tables:
Users
user_id name email
Images
image_id user_id url
user_id and user_id from images table would be linked with a relationship.
Now what I would do is select the user by their Id, check if the user is found, if so then make another query to images table, and check for num rows and loop through the return, is there a function that I could use that would allow me to just select the user and all the images that are linked to the user without doing a joint query.
Thank you for any help
When you say "without doing a joint query" I think you mean "without doing two queries."
In fact, what you want is probably a LEFT JOIN. The idea is that you select users from the user table matching some ID, and LEFT JOIN the images table. The left join will give you null values if no images exist for the user. If you use a normal join, the fact that no matching records exist in the images table will result in no rows returned.
Here is an example:
SELECT u.name, u.email, i.url
FROM Users u
LEFT JOIN Images i ON (i.user_id = u.user_id)
WHERE u.id = #SpecificUserID;
Assuming the user id is found and there are some images for that user, you will get a result that looks like this:
name email url
----- ----- -----
John j#a.com abc.jpg
John j#a.com def.jpg
John j#a.com ghi.jpg
Now as you can see, the name and email values keep repeating. You get a unique image url for each row and the matching username and email.
If you only select one user at a time, this is simple to process in a loop. On the first iteration read all three values. On subsequent iterations just read the url, adding it to your list or array.
Here is a useful tutorial on joins: Understanding JOINs in MySQL and Other Relational Databases
This can be done in one query rather than two by using an inner join to get your result set.
$sql = "SELECT u.user_nid, i.url
FROM tbl_user u
INNER JOIN i.user_nid = u.user_nid
WHERE user_nid = ?"
With this query you will receive a list of the users images and if there are no images returned or the user does not exist, than you will have a row return of zero.
You'll have to use a join to retrieve data from multiple tables in a single query.
The foreign key relationships enforce constraints. Ex: You can't insert a record into Table A referring to a key in Table B without the record actually being in Table B.