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 = ...
Related
I am currently making an android app which uses a feed to display statuses made by users. I have three tables within the same database, each has username either as a primary or unique key column, but each table has different information relating to that user.
For instance, the first table ===>> tbl_users:
username
fname (first name)
mname (middle name)
lname (last name)
etc. (the list is long)
The second table ===>> tbl_userprofilepictures:
profilepictureID
username
profilepicturepath
The third table ===>> tbl_user_feed:
postID (the status' unique ID)
username
status
imagepostpath (the path to the image uploaded with the status)
timestamp
I want to be able to search for the username across all three tables and display the relevant information relating to them on their post. For example I will need their name and surname for tbl_users and I will need their profilepicturepath for tbl_userprofilepictures as well as their status, imagepostpath and timestamp from tbl_user_feed.
Would I need to do this in a seperate PHP file or in the app itself? PS I'm fairly noob at PHP so please feel free to help a bro out.
May the force be with you.
You can use JOIN.
What is JOIN ?
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
Source : w3schools.com Joins
Here is the sample I made base on your tables given. For these one our common field is username.
SELECT CONCAT('a.fname', 'a.lname'), b.profilepictureID, c.status, c.imagepostpath, c.timestamp
FROM
tbl_users as a
LEFT JOIN tbl_userprofilepictures as b ON b.username = a.username
LEFT JOIN tbl_user_feed as c ON c.username = a.username
Using Alias (table_name as custom_name) is a good practice in joining the tables
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]
In my application I have 2 tables in the DB (MySQL):
Companies and News. Company has many News.
Company can have a set of pictures which will be displayed on the company "view page" alongside with all relative information about this company.
I've added a Photos table with next fields: id company_id filename
My question is: Now I also need to have pictures which will belong to News.
I should add another table, which will be called for example Media or I should add additional field (type) to my Photos table, rename company_id to foreign_id and then filter results in PHP and build more complex queries with for example AND 'type' = 1 to fetch photos related to Company and 'type = 2' to fetch photos related to news.
What is a better approach?
You should take the company_id field out of the Photos table and create two new tables, CompanyPhotos with id, photo_id, company_id fields, and another NewsPhotos with id, photo_id, news_id.
Then if you want to get the photos for a company you can do: select * from Photos p inner join CompanyPhoto cp on p.id = cp.photo_id where cp.company_id = ?.
And similary with NewsPhoto: select * from Photos p inner join NewsPhoto np on p.id = np.photo_id where np.news_id = ?.
It is always good to normalize databases. In the beginning it was just about tables with all data and it has evoluted to linked tables with common fields.
Hence, I strongly recommend you to have the table Photos.
After all, you have to make the basic question: can a photo belong to different news? Can a news have different pictures? If both questions' answer is "yes", you have a N:M relation, which is resolved with a middle table containing an id from every table.
You could use UUIDs as your primary key. Because they are unique application-wide (if you create them with CakePHP), you could just use a parent_id column in your Photos table and get rid of the type colum.
Another approach would be MySQL Views. You could setup 2 Views (e.g. NewsPhotos, CompanyPhotos) on top of the Photos table. CakePHP handles (simple) Views like tables, so you could create easily Models & Controllers for this.
Question: I want cumulate or combine many comments into one if they are posted in similar time by the same user. What is the best way of approaching this?
Currently: I have DB table that has comments written by each user, structure as follow:
ID, comment, timestamp, userid
Example: All users have a current status page, here you can view what they have been upto. For a example if the user uploads a new photo, comment will appear to show they have uploaded a photo. But if the user uploads many photos at once, let say 10 in the last 30mins, then there will be 10 different comments, but i want to combine or cumulate this into one comment instead of spamming the status page.
Technology used: MYSQL, PHP
thanks, if more info needed please ask.
you should not create multiple comments if it is all the same
make 3 tables:
photos [id, src, title, date, etc...]
comments [id, comment, timestamp, userid, etc...]
photos_comments [id, photo_id, comment_id]
when you upload multiple comments at once you will create only 1 new record in the comments table, but connect it to multiple photos in the photos_comments table (this is called many to many relationship)
when you want to get all the comments that related to a photo (lets say photo with id=10), you can pull it from the DB with this query:
SELECT * FROM comments WHERE id IN (SELECT comment_id FROM comments_photos WHERE photo_id=10)
or even better, with joins:
SELECT comments.* FROM comments INNER JOIN photos_comments ON photos_comments.comment_id=comments.id WHERE photos_comments.photo_id=10
I need to know how to link two tables in a php/mysql set up then rank the results?
Here is my situation.
I have a stories table:
storyid
writerid
title
story
submitdate
and a votes table
voteid
userid
storyid
vote
I store a vote up as a 1 and a vote down as a -1
I am looking for a way to join these two table then rank/sort the stories by the number of votes they recieve.
I am open to any ideas about how to do so or a different possible database schema.
I prefer to keep the names of my tables singular. It's not a "Stories" table; it's a "Story" table with multiple rows.
A vote can only be attributed to a single story, so it's a one-to-many relationship between the two. I'd put the foreign key in the votes table and let it point out the story it's associated with. Change your schema if you agree: remove the voteid from the story table and make storyid in vote a foreign key to the story table.
But with that said, perhaps you can try a query like this:
select stories.storyid, sum(vote=-1) as down, sum(vote=1) as up
from stories
inner join votes on (stories.storyid = votes.storyid)
group by stories.storyid
Corrected per ypercube's comment below.