Multiple table inner join need first table id - php

Working that query. Than i need projects table's id. echo $row['id'] is printing users->id. But i need print projects->id.
SELECT * FROM projects INNER JOIN users ON projects.manager = users.id

You can create alias like below:
SELECT *, projects.id as prodict_id, users.id as user_id FROM projects INNER JOIN users ON projects.manager = users.id
then you can use it
$row['prodict_id']

Related

Mysql join on three tables to get list of unique data

I want to show all those users from Users table, whose id is present in table favorite_group_users as user_id for a user whose favorite_groups.id = favorite_group_users.id.
I used below query but it is returning null.
select users.id as user_id, users.first_name as name,
favorite_groups.id as group_id,
favorite_groups_users.user_id as carrier_id
from users
inner join favorite_groups
on users.id = favorite_groups.user_id
inner join favorite_groups_users
on favorite_groups.id = favorite_groups_users.favorite_group_id
where users.id = 38;
You may try nested select
select
favorite_groups.group_id, users_group.user_id
from
favorite_groups_users ,
(select
favorite_groups_users.favorite_group_id,
users.user_id
from
users, favorite_groups_users
where
users.id = 38 and
users.id = favorite_groups.user_id
) users_group
where
users_group.favorite_group_id=favorite_groups.group_id

how write sql query with multiple join?

I have fours tables and I wanted to join all three tables with the one table.
I have listed my problem as follows:
Tables:
users
user_training
user_courses
user_certificates
I wanted to get the data from [2,3,4] tables that user_id field matches with the users table ID field.
When I try the INNER JOIN it gives me the result for users that are common in all the tables, But I just wanted to check the [2,3,4] tables with the table [1] Records.
My Query...
SELECT A.training_name AS 'training_name', C.course_name AS 'course_name', D.certificate_name AS 'certificate_name'
FROM user_training AS A INNER JOIN users AS B ON A.user_id=B.ID INNER JOIN user_courses AS C ON B.ID = C.user_id INNER JOIN user_certificates AS D ON B.ID = D.user_id;
Thanks in Advance.
use left join
select u.* from users u
left join user_training ut on ut.user_id=u.user_id
left join user_courses uc on uc.user_id=u.user_id
left join user_certificates uct on uct.user_id=u.user_id
With this one you are getting all users and their respective trainings:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
Changing *_trainig to *_courses or *_certificates will return all users with respected courses or certificates.
If you need to get data in one query, try this one:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
LEFT JOIN `user_courses` ON `users`.`id` = `user_courses`.`user_id`
LEFT JOIN `user_certificates` ON `users`.`id` = `user_certificates`.`user_id`
If user has no trainings, courses, certificates all remaining fields will be null-ed.

Difficulty with join 3 tables in a query in php

My database has 3 tables i wish to access in the select query but I cannot seem to get it to work. Selecting from 2 tables works fine so I know everything else is working apart from my code for selecting from 3 tables. My database has been created on PHPmyadmin
The Tables are as follows:
forum_replies
reply_id
topic_id
user_id
reply_text
reply date
forum_topics
topic_id
category_id
user_id
topic_title
topic_description
topic_date
users
user_id
username
This is the code I have tried to use and shows the fields I wish to select:
$queryreply = "SELECT forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id,
forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username
forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date
FROM forum_replies
JOIN forum_topics
ON forum_replies.topic_id = forum_topics.topic_id
JOIN users
ON forum_replies.user_id = users.user_id
";
$result = mysql_query($queryreply) or die (mysql_error());
$row = mysql_fetch_array($result);
Example in code would be appreciated. Thanks
Use this query:
SELECT a.reply_text, a.reply_date, b.topic_title, c.username
FROM forum_replies a
LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
LEFT JOIN users c ON a.user_id=c.user_id
// apply WHERE, ORDER, GROUP if needed
Apart from syntax errors, you should use LEFT JOIN and table alias in your case.
To show also the topic creator's username, you can adjust the query to the following:
SELECT a.reply_text, a.reply_date, b.topic_title, c.username AS reply_user, (SELECT username FROM users WHERE user_id=b.user_id) AS topic_creator
FROM forum_replies a
LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
LEFT JOIN users c ON a.user_id=c.user_id
// apply WHERE, ORDER, GROUP if needed
You miss the , after users.username..
SELECT forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id,
forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username,
forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date

Inner Join with loosely related tables and multiple relations

In a table if I have:
FixtureID, HomeTeam, AwayTeam
Can I replace the ID that is related to HomeTeam and AwayTeam when it displays in the browser? HomeTeam and AwayTeam are both related to TeamID in the Teams table.
I want to show all fixtures and then replace both "TeamID" with "TeamName" so the name shows up instead of the ID?
So far I have:
$sql = <<<SQL
SELECT fix.*, tea.*
FROM Fixtures fix
INNER JOIN Teams tea USING (TeamID)
SQL;
Then
echo '<p>Fixtures</p>';
echo '<div>'.$row['HomeTeam'].' v '.$row['AwayTeam'].'</div>';
EDIT:
Ok so I found a post that was similar to what I need and have tried the following:
$sql = <<<SQL
SELECT fix.*, tea1.*, tea2.*
FROM Fixtures fix
INNER JOIN Teams tea1 ON fix.HomeTeam = tea1.TeamID
INNER JOIN Teams tea2 ON fix.AwayTeam = tea2.TeamID
SQL;
However it's still just showing the team ID's rather than the names from the Teams table.
Solved it myself
$sql = <<<SQL
SELECT f.*, t1.TeamName 'HomeTeam', t2.TeamName 'AwayTeam'
FROM Fixtures f
INNER JOIN Teams t1 ON f.HomeTeam = t1.TeamID
INNER JOIN Teams t2 ON f.AwayTeam = t2.TeamID
SQL;

Select a row from two tables depend on primary key(Mysql)?

I have two table users and album. In users table there is user_id primary key .In other table albums there are multiple rows with that user_id because every time when a user upload a new album it uploads with user_id as foreign key. I want to select only once the user_id with other table(album) ignore other result set.
How can I achieve this?
SELECT a.*, b.*
FROM users a
INNER JOIN album b
ON a.user_ID = b.user_ID
INNER JOIN
(
SELECT user_ID, MAX(photo_id) max_rec
FROM album
GROUP BY user_ID
) c ON b.user_ID = c.user_ID AND
b.photo_id = c.max_rec
SELECT album.* FROM album LEFT JOIN users ON user.id = album.id WHERE user.id = SOMEIDHERE
I believe this will work, your not giving me a whole lot of info to work with.
SELECT *
FROM ( SELECT u.*, a.*
FROM users AS u
INNER JOIN album AS a
ON u.user_ID = a.user_ID
ORDER BY a.created DESC) AS h
GROUP BY user_ID
ORDER BY b.created DESC -> ORDER BY whatever row you wish for. In this case the newest one is chosen.

Categories