$query="SELECT distinct city_name,donnor.donor_id FROM city join bloodgroup ON city.city_id = bloodgroup.city_id join donnor ON bloodgroup.blood_id = donnor.blood_id";?
i want to count how many time city apear in integer..
Have a look at group by and aggregation (count) within select.
It might look something like:
(edited)
select count(city_id)
from city
group by city_id.
That's of cause a minimalistic example without the joins. Also you don't need distinct when you try to count.
Related
I have a problem with an SQL query. This is my first time using advanced SQL operations like this so it could be that I'm missing something basic. I am running this query:
SELECT countries.id,
countries.name,
AVG(users.points) AS average
FROM countries
LEFT JOIN users
ON countries.id = users.country
ORDER BY average DESC
This query is only returning 1 row and it's not following the ORDER BY because the returned value is . My aim with this is to get all the records in the Countries table and get the average of the points awarded to the users from each country. I want it to return those countries which do not have users assigned to them as well. I have done this in 2 queries and it worked but I thought that maybe I could do only one query. What am I missing?
It is only returning one row because it is an aggregation query without a group by. Perhaps you mean:
SELECT c.id, c.name, AVG(u.points) AS average
FROM countries c LEFT JOIN
users u
ON c.id = u.country
GROUP BY c.id, c.name
ORDER BY average DESC;
The AVG() makes this an aggregation query. Without the the group by, SQL interprets it as returning one row summarizing all the rows. MySQL supports an extension to the SQL standard where columns in the select do not have to be in the group by. In most databases, you query would return an error.
I need help with an advanced SQL-query (MSSQL 2000).
I have a table called Result that lists athletics 100 meter race-times. A runner can have several racetimes but I want to show only the best time from each runner.
The Result-table contains three columns, Result_id, athlete_id, result_time. So athlete_id must be unique when I list the values and result_time must be the fastest (lowest) value.
Any ideas?
In SQL Server 2000, you can't use windows functions. You can do this as follows:
select r.*
from result r join
(select athlete_id, min(result_time) as mintime
from result r
group by athlete_id
) rsum
on rsum.athlete_id = r.athlete_id and r.time = rsum.mintime
In more recent versions of SQL Server, you would use row_number().
If you simply need the fastest time for each athlete_id, do this:
select athelete_id, min(result_time) as FastestTime
from result
group by athelete_id
To show additional columns from the result table, you can join back to it like this:
select r.*
from result r
inner join (
select athelete_id, min(result_time) as FastestTime
from result
group by athelete_id
) rm on r.athelete_id = rm.athelete_id and r.result_time = rm.FastestTime
What you want is to use an aggregate function. in this case min() which will select the minumin data from all the rows that have the same data in the other selected columns. This means you also have to us the group by clause. The query below should give you the results you want.
Edit: If you need other columns, just bring them into the select clause, then add them to the group by clause like below:
select althlete_id, result_id, min(result_time) as result_time from result-table group by althlete_id, result_id
select althlete_id, result_id, min(result_time) as result_time, race_date from result-table group by althlete_id, race_date, result_id
Edit: You need to add all the columns into the group by that aren't part of an aggregate function. Aggregate functions are ones like min(), max(), avg() and so on.
Short answer: If you aren't putting a column in brackets, it probably has to be in the group by.
I have the following results for my database table:
The Query:
SELECT
service_titles.user_id, service_titles.slide_id, service_titles.name as title_name ,service_names.name as service_name
FROM service_names
INNER JOIN service_titles ON service_names.title_id = service_titles.id
So what needs to happen is:
If the user has 2 unique service titles, then the max number of service_names for that title will be 6
If the user has 1 service title, the the max number of service_names for that title will be 16
I will be using PHP for all of the coding, but I am wondering how I would go about this. I need a way to count how many unique service_titles there are for that user and slide, and then count how many service items there are for each title.
Thanks for any help!
SELECT COUNT(DISTINCT service_titles.name)
FROM service_names
INNER JOIN service_titles ON service_names.title_id = service_titles.id
GROUP BY service_titles.user_id, service_titles.slide_id
That'll get you the number of distinct title_names for each user_id/slide_id combo.
SELECT COUNT(DISTINCT service_names.name)
FROM service_names
INNER JOIN service_titles ON service_names.title_id = service_titles.id
GROUP BY service_titles.user_id, service_titles.slide_id
... and that's the number of distinct service_names for same. If you want both in one query, you can put both COUNTs together, since you're using the same GROUP BY regardless.
You could use a CASE statement within your query to change the max number of service_names.
See MySQL CASE statement reference
To do this in the SQL itself would be quicker than evaluating it in PHP.
To count how many distinct titles you can try:
SELECT user_id, COUNT(DISTINCT name)
FROM service_titles
GROUP BY user_id
I want to join 2 databases together and work out the sum of each one with the same ID from both tables, if you get where im coming from...
I have one database with ID, product_name, product_description
and the other has ID, stock amount, date_added
i have joined the databases together using the JOIN function and it displays them correctly, but for each entry into the database it adds another row when i add stock and shows another row at the frontend. I would like it to group all of the same id's into one row.
I have tried this:
$result = mysql_query("SELECT * FROM site_products JOIN site_trans ON site_products.product_id = site_trans.trans_product GROUP BY site_products.product_id");
it groups the same id's together into one row, but the stock amount just displays the last amount added, not the total amount.
i was thinking about adding the SUM() to this but im not sure where it would go in the query.
From sqlcourse2.com
The GROUP BY clause will gather all of the rows together that contain data in the
specified column(s) and will allow aggregate functions to be performed on the
one or more columns.
You can read more about GROUP BY with aggregate functions usage in following links:
w3schools.com
SQL: GROUP BY Clause
GROUP BY clause
MySQL GROUP BY - Aggregate Functions
Now I think it will be simple to understand the following query which solves your problem:
SELECT
site_products.product_id,
SUM(stock amount) AS total_amount
FROM site_products JOIN site_trans
ON site_products.product_id = site_trans.trans_product
GROUP BY site_products.product_id
SELECT site_products.*, SUM(site_trans.stock_amount) AS total_stock FROM site_products JOIN site_trans ON (site_products.product_id = site_trans.trans_product) GROUP BY site_products.product_id
This should work:
$result = mysql_query("SELECT site_products.Id, site_products.product_name, site_products.product_description, SUM(site_trans.stockamount) FROM site_products JOIN site_trans ON site_products.product_id = site_trans.trans_product GROUP BY site_products.product_id, site_products.product_name, site_products.product_description");
SELECT DISTINCT business.name AS businessname
,business.description AS description
FROM business
, category
, sub_categories
WHERE business.cityID = '$city'
AND (category.name LIKE '%$name%'
OR sub_categories.name LIKE '%$name%')
AND business.status = 0
Pls the above SQL code is suppose to search a set of two tables the ones in the bracket and return the result, but for some reason, it's not doing so. What am i doing wrong?
Thank You.
Your query would produce a cartesian product. Depending on the size of your tables that could take a considerable amount of time.
Based on your clarification I'd use a subquery to check for matching categories, this way you don't have to use distinct in your query as it would only return each business once. I also suggest you to start with a decent SQL tutorial.
SELECT name AS businessname
,description AS description
FROM business
WHERE cityID = '$city'
AND status = 0
AND ( categoryID in (select id from category where name like '%$name%')
or subcategoryID in (select id from sub_categories where name like '%$name%')
)
Two things come to mind:
You are not joining any of the three tables together. Consider adding a few LEFT JOIN clauses.
You are selecting columns from only one table. If you wanted columns from other tables, you should add them to your SELECT clause.