PHP - MySQL- Get names from other tables with an id - php

I have a mysql database with multiple tables that describe playlists and users(each of these tables has an ID and Name for the playlist/user).
Now, i have one table "PlaySent" that has only IDs that point to one playlist and two users (a recipient and a sender).
My objective is to Select all records from this table (PlaySent) where i don't get these IDs but the names they represent.
I believe i have to use an INNER JOIN?
EDIT: I have made a sketch of the tables in question, so they can be in english
So again, i need a query that returns me all data from PlaylistSent where it doesn't show me the id, but the names of the playlist and the users in each row

Answer
Your Query will look like this, you have not posted the exact name of the fields so i am just assuming.
SELECT pl.PlaylistName as playlist_name,sender.Username as sender_name,receiver.Username as receiver_name
FROM PlaylistSent AS ps
INNER JOIN Users AS sender
ON ps.1stUserID=sender.id
INNER JOIN Users AS receiver
ON ps.2ndUserID=receiver.id
INNER JOIN Playlist AS pl
ON ps.PlaylistId=pl.id
Off topic
I would suggest you next time to:
1) include the exact database structure
2) include any SQL you tried and did not work and the output of the SQL
3) include a small sample of data
Also it would be helpfull for you to start by reading a book or a tutorial to learn the syntax and get a deeper understanding on the topic you try to learn

Related

Displaying INNER JOIN results as HTML table on webpage?

I have the following query:
$sql = "SELECT first, COUNT(FIRST) FROM techs INNER JOIN ros ON techs.id = ros.writtenby GROUP BY ID";
That gives the desired output of:
first
COUNT(FIRST)
Eric
88
John
11
I have no earthly idea how to display this EXACT table onto a PHP/HTML page. I've created tables based off of queries but it's basically just displaying database table data. I have a feeling the INNER JOIN is the root of my issue since it's not an actual table in the database, rather just results from my query. If this isn't apparent enough already I'm very much a beginner at coding in general so be kind. Lol.
From comments:
I don't understand how to call and then display the "count(first)" column of my query results, since "`count(first)" isn't an actual column in either of the tables I am generating the result from.
If only that's your problem, use alias, like:
SELECT first, COUNT(FIRST) AS my_count FROM techs INNER JOIN ros ON techs.id = ros.writtenby GROUP BY ID
Note that above is the query you posted, just with " AS my_count" added.

PDO Two Table Joining

First table:
Second table:
In the first Table, there will be multiple given_to with same taskid, and specific to that taskid i have set task in 2nd table.
Is it possible to obtain the task of a same taskid from multiple users to be printed in a table? If so How can we achieve it?
If possible, I also want to print the columns given_to of the task seperated by space.
Please Help
I'm not exactly sure I understood correctly what you want as a result, but as far as I get it you can achieve this with the correct SQL query, using a simple left join:
SELECT * FROM table1 LEFT JOIN table2 ON table1.taskid = table2.id
You might want to replace the SELECT * FROM ... part with the specific fields you are interested in.
For more information about joins (that is: merging results/columns from several tables into one query result) take a look at the MySQL reference manual on JOIN syntax.

MySQL join 2 tables?

I have a database with users. Now I'm trying to create a wall (Facebook like).
I've added a table in my db called status_update and added a row username(user who posted the status). Then I created a query to search for statuses from the user on whose profile page you're on (select from status where username=$_GET['profile']).
I'm wondering how smart this is? Is there a way to use JOIN or something? Now this was the only logic solution that came to my mind and I know how to make it.
Also this is fine for this page but when I'll create a feed page for those who are "Friends or Followers" that's gonna be a bit tricky.
Is there a better solution for this?
yes you can join two tables by there index or any value, just do it like this
select * from table1 inner join table2 on table.field=table.field
it depends what join you want, inner join if data present on both table other wise left if data may or may not present

How To Merge Array Data From Database

I have a database table for a user of my website, this table gives each user a user_id.
Using normalization, I have linked the user to a group with a user_group table including user_id and group_id to link a user to a group.
I then have a group table that links a group name to the group_id.
I am trying to output the users on my webpage in a list next to the name of the group, not its id.
I was thinking of using a foreach loop to do this, but the data would need to get into one array? I dont know how I would take the user_id, find out which group _id it it paired with, find out which group name that id was paired with, and then add that to the array with the names and be able to display the group name next to the user's name using foreach.
Thanks for any help.
If i understand you correctly, you want to join you users data to group to show the user details with their group name.
Try out this:
select u.*,g.*,ug.* from users u
left join user_group ug on u.user_id=ug.user_id
left join `group` g on ug.group_id=g.group_id;
From above query you may get blank values for some user's, who are not assigned to any group.
I don't see why a left join is necessary. Should be a regular join unless he wants to display users that don't have groups.
By the way, this is the perfect example of something that should be done in the DB with a select. Pulling everything from these tables and trying to sort through them in java is not a good idea. Maybe I read this post wrong though.

Mysql join query retrieving profile images of users with ids from other table

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

Categories