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
Related
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'm building library which generates SQL queries and found case which i don't understand why it is happening so. I agree that in general you should filter out by "where" and not "on" yet ON allows to filter out for LEFT join. Though for RIGHT join i get results that do not match ON request. I wonder why RIGHT join works this way.
Users (table name _user):
Invoices (table name _invoice):
Query:
SELECT
_user.id AS userId,
_user.name AS userName,
_user.state AS userState,
_invoice.id AS invoiceId,
_invoice.userId AS invoiceUserId,
_invoice. `state` AS invoiceState
FROM
_user
RIGHT JOIN _invoice on _invoice.state = 'pending'
Response:
Question:
Would be best to get step-by-step execution for this query-result to understand how exactly it happen.
A right join keeps all rows from the second table regardless of whether the on clause evaluates to "true", "false", or NULL.
Presumably, though, you want a valid join condition. My guess is you want a list of all users and any pending invoices. If so, the correct logic would be:
SELECT . . .
FROM _user u LEFT JOIN
_invoice i
ON i.userid = u.id AND i.state = 'pending';
At the very least, this produces a result that seems usable.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I am having some trouble writing this SQL Query.
Basically, I have the following tables;
received_flight_files_table & uploaded_flight_files_table
And the following fields;
Processing_Month, IATA_Code, Airline, Received_Date, Uploaded_Date
I would like the query to produce something like the first attached screenshot.
The 2 date fields you can see are from different tables though.
The join can be done on the IATA_Code and Airline fields as both of these values should be the same in both tables.
Here are 2 examples of statements I have written which aren't quite right;
SELECT DISTINCT received_flight_files_table.Processing_Month, received_flight_files_table.IATA_Code, received_flight_files_table.Airline, received_flight_files_table.Received_Date, uploaded_flight_files_table.Uploaded_Date, published_flight_table.Published_Date
FROM ((received_flight_files_table
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Processing_Month=received_flight_files_table.Processing_Month)
INNER JOIN published_flight_table ON published_flight_table.Published_Month=received_flight_files_table.Processing_Month
WHERE received_flight_files_table.Processing_Month = [enter MMMYY];`
SELECT DISTINCT received_flight_files_table.Processing_Month, received_flight_files_table.IATA_Code, received_flight_files_table.Airline, received_flight_files_table.Received_Date, uploaded_flight_files_table.Uploaded_Date
FROM (((received_flight_files_table
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Processing_Month=received_flight_files_table.Processing_Month
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.IATA_Code=received_flight_files_table.IATA_Code
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Airline=received_flight_files_table.Airline)))
WHERE (received_flight_files_table.Processing_Month = [enter MMMYY];
There should sometimes be some blanks in the Uploaded_Date field as for example - in one table I might have received the files but not yet uploaded them.
Try to use LEFT JOIN instead of INNER JOIN.
https://www.diffen.com/difference/Inner_Join_vs_Outer_Join
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).
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)
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...