Group by MySQL with special case [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 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).

Related

Getting data from one table if not also in another [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 got a database with two relevant tables (times and registrations).
I want to get the times from the times table, through an SQL query, that aren't in the registrations table (don't have a registration on them). Registration at 5PM, wouldn't show 5PM from times in the query.
My current query is:
SELECT time FROM `times` WHERE time IN (SELECT time FROM `registrations` WHERE ID IS NOT NULL)
(registrations DO have an ID)
This does the opposite of what I want it to do (shows all times, regardless of registration, or not).
How could I get the opposite effect?
You seem to want not exists:
select t.*
from times t
where not exists (select 1 from regisrations r where r.time = t.time)

SQL Query how to add non existing column [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
Hello guys i need help im stuck dont bully me for this i know it can be easy for you but im just bad at thinking lol :D read few times the manual on w3school and etc but still dont understand this nonsense
i need and sql query which can count how many real estate objects agent has , the database that im currently working on sql is about real estate its in lithuanian , the task is to list agent name(AgVardas), surname(AgPavardė) , phone (AgTelefonas), agency name(AgentūrosPavadinimas), and real estate objects(Nekilnojamuobektuskaicius) that agent has. The names in () these brackets are original values in my native language (Lithuanian)
i have to use the query with inner join
because its in other tables
the databases
https://imgur.com/a/qa1JUWt
the code query i have tried is
SELECT `AgPavardė`,`AgVardas`,`AgTelefonas`,`AgentūrosPavadinimas`
FROM `agentai`
INNER JOIN agentūros ON agentūros.AgentūrosNr = agentai.AgentūrosNr
it works and all but i need to add another column to show me Real estate objects that agent owns(number)
example how it should look
https://imgur.com/a/TMAARKC
So the question is how to do so?
You can replace the column and table names in the below query with your Lithuanian names and it should work.
SELECT agent.Agent_Name, agent.Agent_Surname, agent.Agent_Phone , agency.Agency_name, COUNT(ad.ad_number) as Real_Estate_Objects
FROM tbl_agent agent
INNER JOIN tbl_agency agency ON agency.agency_number = agent.agency_number
INNER JOIN tbl_ads ad ON ad.agent_number = agent.agent_number
GROUP BY agent.Agent_Name, agent.Agent_Surname, agent.Agent_Phone , agency.Agency_name
The easiest would be to select AgentId and count of the agent's real estate objects first. You do so like this.
SELECT AgentūrosNr, COUNT(Nekilnojamuobektuskaicius) as AgentObjectsCount
FROM agentūros
GROUP BY AgentūrosNr
Then join the result with agent's info and select info columns + count column
SELECT `AgPavardė`,`AgVardas`,`AgTelefonas`,`AgentūrosPavadinimas`, AgentObjectsCount
FROM `agentai`
INNER JOIN (
SELECT AgentūrosNr, COUNT(Nekilnojamuobektuskaicius) as AgentObjectsCount
FROM agentūros
GROUP BY AgentūrosNr
) agentsWithCount
ON agentsWithCount.AgentūrosNr = agentai.AgentūrosNr

Not sure on Left Join, Outer Join, Unions or Minus? [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 5 years ago.
Improve this question
Current work requires my to have a complex level of SQL syntax including these examples.
What in short is a left Join and where would you use it?
What ever you have written is a JOIN clause that is used to combine rows from two or more tables, based on a related column between them.
Including outer join or left join depends on the table structure and the requirement to pull data from those tables.
IF you are looking for what is left join ,
Left join returns everything from the left table and the matching records from the right table and if there is no match in right table the result is null for the right table
Let us know what are you looking for

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 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...