mysql query for the following problem? [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have three tables say 'user', 'user_resources' and 'desktop_resources'.
'user' contains - emp_id,name and other attributes
'user_resources' - emp_id and desktop_id foreign key relation
'desktop_resources' - desktop_id and other attributes
Now i want a sql query from where i can get a table which shows me name from 'user' table and 'desktop_resources' attributes only where "emp_id=d_id"
how to go about it??

I don't see d_id column there, but if you think so, it would look like this:
SELECT name, desktop_resources.*
FROM desktop JOIN user_resources USING (desktop_id) JOIN user USING (emp_id)

This is a straightforward series of joins:
select u.name, dr.*
from user u
join user_resources ur on ur.emp_id = u.emp_id
join desktop_resources dr on dr.desktop_id = ur.desktop_id
where u.emp_id = $d_id

Finally found this query useful:
SELECT name, desktop.*
FROM desktop
NATURAL JOIN (
user
JOIN user_resource ON user.emp_id = user_resource.emp_id
)
I am sure there may be other ambiguous queries for this..if u have got a better query..please put it in comments...

Related

MYSQL replace id with name from another table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've been having a lot of trouble with a Join statement that I can't get to work. I'm relatively new to MySQL so would appreciate some help.
I have 2 tables under the database "CoreProtect", co_command and co_user. co_command has 2 columns, message and user. co_user has another 2 columns, "rowid" and "user". rowid refers to the "user" column in co_command, where the "user" from co_command is the same as the "row_id" in co_user, the user field in co_user stores the actual string name of the user. (I hope that makes sense)
The query I am using is:
SELECT user
COUNT(message)
FROM co_command
GROUP BY user
HAVING COUNT(message)
ORDER BY count(message)
This does what I expect, however it gives the numerical value that refers to the other table of the names. I'd like the output to give the Name of the User and the count, instead of the numerical refrence to the other table.
I know I need a JOIN statement for this and I've researched it but have been unable to make it work.
I can find the string username from the other table with
SELECT user
FROM co_user
WHERE rowid="User Value from co_command";
Thanks for any help.
seems you need a join
SELECT c.user, u.user
, COUNT(message)
FROM co_command c
INNER JOIN co_user u ON. u.rowid = c.user
GROUP BY c.user, u.user
ORDER BY count(message)

How to select specific columns from different tables in mysql database in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have 3 tables:
request(RequestID,Name,Mobie Number,Vehicle Number,Location)
reqresponse(RequestID,ResponseID,MechanicID,MechanicName)
mechdetails(MechanicID,MechanicName,MechanicMobile,MechanicAvalability)
Now, what I want to do is select all data from the request table and display it along with details of the mechdetails table, where MechanicAvailability is set to 'busy'.
RequestID is the foreign key in reqresponse table.
Can anyone please tell me the MySQL code to do so.
I am working in PHP.
This is where I am right now:
SELECT * FROM mechdetails WHERE MechAvailability='Busy' AND
MechanicID=(SELECT MechanicID FROM reqresponse WHERE ResponseID='$rid')
I am really new to MySQL and PHP so please help me!
The query
SELECT * FROM request r
JOIN reqresponse q ON q.RequestID = r.RequestID
JOIN mechdetails m ON m.MechanicID = q.MechanicID
WHERE MechanicAvailability = 'busy'
should do the job.
To access the resulting data, use mysqli_query and mysqli_result.
You can join the tables:
select r.*, m.*
from request r
inner join reqresponse rr on rr.requestid = r.requestid
inner join mechdetails md on md.mechanicid = rr.mechanicid
where md.mechanicavailability = 'busy'

mySQL select every record that does not exist in another table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have 3 tables:
users (id_user)
achievements (id_achievement)
user_achievements (id_user, id_achievement)
I need help selecting every achievement that a user has not yet completed.
Consider a cross join that generates all combinations of users and achievements, and a not exists condition with a correlated subquery to filter out those that were completed already:
select u.id_user, a.id_achievement
from users u
cross join achievements a
where not exists (
select 1
from user_achievements ua
where ua.id_user = u.id_user and ua.id_achievement = a.id_achievement
)

Select all rows from MySQL table with non-existent relation as empty cell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If I had for example two tables called 'teachers' and 'lessons' and 'lessons' has a foreign key 'teacher_ID' referring to its counterpart in the 'teachers' table, then how would I select all rows from teacher with all of their corresponding lessons with an empty cell if no lesson is connected to them? I only was able to make MySQL show me the teachers that have one or more lessons attached.
Is this even possible without LEFT JOIN? I couldn't find anything on Google...
EDIT
I was interested in the mechanics of the LEFT JOIN keyword. But since there doesn't seem to be an alternative I'd say case closed.
The right way is using LEFT JOIN. This way if not match you will get (teacher_id), null
The LEFT JOIN keyword returns all the rows from the left table (teachers), even if there are no matches in the right table (lessons).
SELECT teacher.teacher_ID, lesson.lesson_ID
FROM teachers
LEFT JOIN lessons
ON teacher.teacher_ID = lesson.teacher_ID
If you want to emulate LEFT JOIN first use JOIN to find the element with match. And use UNION to add the rest with a value of NULL
SELECT teacher.teacher_ID, lessons.lesson_ID
FROM teachers
JOIN lessons
ON teacher.teacher_ID = lessons.teacher_ID
UNION
SELECT teacher.teacher_ID, null as lesson_ID
FROM teachers
WHERE NOT EXISTS ( SELECT 1
FROM lessons
WHERE lessons.teacher_id = teacher.teacher_id)

Linking two MySQL tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Even this is asked a billion times here already, none of them has worked for me.
So: I have two tables:
banhammer_bans:
and banhammer_players:
What sort of query should I make that it gets the name value from the "players" table corresponding to the "player_id" and "creator_id" value? I've tried with JOINS and UNIONS but no success.
Select p.name, p.id as player_id, b.creator_id as creator_id
from banhammer_bans as b
inner join banhammer_players as p on p.id = b.player_id
You can use something like this:
select
*
from
banhammer_bans, banhammer_players
where
banhammer_players.player_id = banhammer_bans.id
This is a kind of Join too, but it has some sort of efficiency problems.