SQL query which returns COUNT value of matched columns of two different tables - mysql

I want to output the number of properties registered in db of same cities under that city. Here is my tables:
What I want is to select number of properties with same city, Mysql command with PHP output all the 3 properties if I run for a specific city.
SELECT COUNT(property_id)
FROM properties
JOIN cities
WHERE cities.city_id = properties.city_id
city_id is a foreign key in properties table, How can I show total properties under same cities?

Related

Get Data From Multiple Tables On the Basis of Column Value From Different Table

I have one Table of users in which their is LOGIN_ID, LOGIN_PASSWORD, and USER_ROLE.
I want to get Name of USERS From Different Table with single query.
e.g.
Login_ID: 1001 is from students so we have to get the name form students.
Login_ID: 235738932 is from Employees so we have to get name from Employees.
There is no Foreign Key Relation in users Tables because Login_id is from two different Tables.
Try:
SELECT
LOGIN_ID,
LOGIN_PASSWORD,
USER_ROLE,
CONCAT(COALESCE(students.name,''),COALESCE(employee.name,'')) as Name
FROM users
LEFT JOIN students ON students.Login_ID = users.Login_ID
LEFT JOIN employee ON employee.Login_ID = users.Login_ID
COALESCE(students.name,'') Will return the students name. If the name is not found, the values will be NULL, and COALESCE will change this to ''.
The same happend to the name of the employee. Both names are getting concatenated.

SQL Query to find the common data based on two or more condition from the same table?

I have set of country codes and each country has set of accounts holder counts.
I want to fetch the same account numbers along with the country codes among all the countries which are being available in multiple countries.
For example:
Count the number of accounts grouped by country codes:
SELECT [Country Code],COUNT([Accounts]) FROM [table] GROUP BY [Country Code]

SQL - Finding Duplicates of One Column But Different in Another Column

How do I find rows that have duplicate values in one column but different values in another column. I have a database of cities with latitude and longitude. I would like to find all rows that have a duplicate in the city name column but have different latitude and longitude. For example, paris,france and paris,texas have the same city name "paris" but different latitude and longitude, so they would both be included in the query results. But it should not return rows that have duplicate city names and duplicate latitude/longitude values.
If you just want the city names:
select city
from table t
group by city
having min(latitude) <> max(latitude) or
min(longitude) <> max(longitude);
If you want the details, then you can join this back to the main table or use group_concat() to assemble a list of countries (for instance).
select city,latitude,longitude,count(*) cnt
from table
group by city,latitude,longitude
having count(*)>1
the cities which come in above resultset are duplicates.

Match data from two tables and return results where something matchs

Update 4/25/13 6:25AM: I am using MyISAM
I have searched a lot and am not sure the best way to do this. I have two tables that have matching values in different columns and need to return all that apply to where clause.
Table 1 name agent
Relevant Column Names agent_name and team
Table 2 name poll_data
Relevant Column Names agent and duid
So I want to count how many poll results I get from each teambut I need to somehow add the team from agent table to poll_data by matching the agent.agent_name to poll_data.name so I can return only data for that team. How can I match the records and then search them in a single query.
try this ...
$query1="SELECT COUNT(*) FROM poll_data JOIN agent ON (poll_data.agent = agent.agent_name) GROUP BY agent.team";
you should normalize the database using foreign key.

What's the mysql syntax to pull data on the fly

I have a table with a bunch of orders... one of the columns is order_status. The data in that column ranges from 1 to 5. Each number relates to a name, which is stored in another table that relates that number to the respective name.
SELECT order_id , order_status FROM tablename1
The above would just return the numbers 1,2,3,4,5 for order status. How can i query within the query on the fly to replace these numbers with their respective names.
Also, what's the term used to describe this. I'd Google it if i knew what the appropriate term was.
Each number relates to a name, which is stored in another table that
relates that number to the respective name.
JOIN it with the other table:
SELECT
t.order_id,
s.StatusName
FROM tablename1 AS t
INNER JOIN the statusesTable AS s ON t.order_status = s.status_id;