MYSQL replace id with name from another table [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 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)

Related

How to solve a complex MySQL query to show the name of two users [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 1 year ago.
Improve this question
I have two tables in my MySQL database:
One table is called "users" and another table is called "assessment_submissions".
The "users" table contains an "id" and a "name" parameter.
The "assessment_submissions" contains an "evaluator_user" parameter and "evaluated_user" parameter, both parameters are related to the users id on the first table.
When I execute the following SQL query:
select * from assessment_submissions
join users
on assessment_submissions.evaluator_user = users.id
where evaluated_user = 6 and evaluator_user = 3
limit 1
The query above gives me the name of the "evaluator_user", but I want to create a SQL query that gives me the name of the "evaluated_user " and the "evaluator_user " in the same row.
How can I do that?
Join the submission list to the user list twice to get both users
SELECT
submission.id,
evaluator.name,
evaluated.name
FROM
assessment_submissions AS submission
LEFT JOIN
user AS evaluator
ON
submission.evaluator_user = evaluator.id
LEFT JOIN
user AS evaluated
ON
submission.evaluated_user = evaluated.id
Join users a second time with another alias.
Something like this:
SELECT
as_sub.id, u1.name, u2.name
FROM
assessment_submissions as_sub,
users u1,
users u2
WHERE
u1.id = as_sub.evaluator_user and
u2.id = as_sub.evaluated_user

MySQL: Show all data from one query and join data from another query where it matches [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
Trying to understand SQL more as part of a POC I'm working on and have hit a snag. I have two select queries (shown below) and I want to combine these into a single query that:
Shows all of the results of query one
Joins the result of query two if the category column matches.
Provides a default of 0 if there is no match
Query one:
SELECT activityId, location, category, activityScore FROM activities WHERE location = "manchester";
Query two:
SELECT userId, category, userScore FROM userscore s WHERE userId = "32976428";
Expected Output:
The resulting query should show all activities in "manchester" along with the associated userScore if the specified use has one that matches that category. If there is no userscore then 0 should be returned instead.
Thanks for any help.
Carl
I think you need a LEFT JOIN on your userscore table
SELECT a.activityId, a.location, a.category, a.activityScore,
s.userId, ISNULL(s.userScore,0) as userScore
FROM activities a
LEFT JOIN userscore s ON s.category = a.category AND s.userId = "32976428"
WHERE a.location = "manchester";

Query from multiple tables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have two tables one named logins and one named members.
Both members and logins have "Name" field. This is the primary key.
Members includes information for accounts including the field "Rank"
logins includes information on last login with a unix time stamp on the field "time"
I need to run a query for the members table that is Rank > 1 but I also need to make sure that the "time" field is more than 1391212800 from the logins table.
How would I do this?
You can use a join to build a recordset from two distinct tables. The on clause allows you to specify the relation between the two tables.
Finally, you can apply a where clause to the new recordset that can refer to fields from both the original tables.
select *
from Members m
join Logins l
on l.name = m.name
where m.Rank > 1
and l.time > 1391212800
select m.name , m.rank from logins l
inner join members m
on l.mame = m.name
WHERE time >= 1391212800
and m.rank > 1

MySQL check if value exist more than once [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have MySQL database and a have a table named CALLER. In table caller I need to check if in column USERNAME there are values which exist more than once and if exist to list all these values.
Thank you
Ususlly you can do this using GROUP BY and HAVING COUNT(*)>1
SELECT USERNAME FROM CALLER
GROUP BY USERNAME
HAVING COUNT(*)>1
Update: To get all duplicate rows:
SELECT * FROM CALLER WHERE USERNAME IN
(
SELECT USERNAME FROM CALLER
GROUP BY USERNAME
HAVING COUNT(*)>1
)
Before getting to the answer....
It would have been a help if you'd provided the full table structure (as a CREATE TABLE statement)
If you need to apply this exercise it implies your database design is wrong - and after you've identified the duplicates and resolved them then you should add a unique index on the relevant column.
Assuming that you've got an auto-increment field, or some other value (such as created date) which differentiates between rows with the same USERNAME....
SELECT a.id, username, b.id
FROM caller a
INNER JOIN caller b
ON a.username=b.username
AND b.id>a.id
Note that this will report some rows more than once if the username exists for more than 2 rows. Alternately:
SELECT username, COUNT(DISTINCT id), MIN(id), MAX(id)
FROM caller
GROUP BY username
HAVING count(*)>1
But this won't explicitly identify all the ids where there are more than 2 rows with a specific username.

mysql query for the following problem? [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
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...