Query from multiple tables [closed] - mysql

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

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)

Group by MySQL with special case [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 4 years ago.
Improve this question
I have two tables called Master Websites and Defacement Monitor
Master website Table
Defacement Monitor Table
The master_website table contains all the website in the system, and defacement monitor status table filled with status of the each website. We can add status of a website with any date and time statuses are 'safe', 'defaced', 'broken', 'normal', What I want is count of the 'safe','normal', 'broken' with considering only the last entry of each website.
Any help please?
Sorry for my bad english
Thanks in advance
You can join the tables together, and then do a LEFT OUTER JOIN against the monitor table looking for a newer row. Using the WHERE clause you can ignore any row where a newer one is found.
Like this:-
SELECT dm1.defacement_status, COUNT(dm1.id)
FROM master_website_table m
INNER JOIN defacement_monitor_table dm1
ON m.id = dm1.website_id
LEFT OUTER JOIN defacement_monitor_table dm2
ON m.id = dm2.website_id
AND dm1.defacement_datetime < dm2.defacement_datetime
WHERE dm2.id IS NULL
GROUP BY dm1.defacement_status
Note that if any status is not currently used then it will not appear in the results (ie, you will not get a count of 0). It would be better to have a table containing the possible status' (then your monitor table could just use the id from this table instead of the text of the status).

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)

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.

Select rows from one table with joined only last row from another [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 9 years ago.
Improve this question
I have 3 tables (match, player and goal),
and I need to display all matches, but only with the name of the last player who scored.
I know that I need to join my tables, but I don't know how.
here is my database diagram:
database diagram http://img843.imageshack.us/img843/582/8gqj.jpg
Or you can do
SELECT m.id, m.date, p.name, g.goal FROM match m
LEFT JOIN ( SELECT max(id) gid,id_match FROM goal GROUP BY id_match )
lastgoal ON lastgoal.id_match=m.id
LEFT JOIN goal g ON g.id=lastgoal.gid
LEFT JOIN player p ON p.id=g.id_player
The first subquery (with a GROUP BY) finds the last goal of each game on the assumption that it has the highest id (entered last), the rest is straight forward joins ...
Use LIMIT mixed with ORDER BY to select only one item from a table, then make your join.