Query to group inner join rows - mysql

I have 2 tables: Projects and custom_values which are linked by the project id.
I did this inner join query
SELECT custom_values.customized_id ,custom_values.custom_field_id,
custom_values.value FROM projects INNER JOIN custom_values ON
projects.id=custom_values.customized_id order by custom_values.customized_id asc
You will find the responce of this query in the file joined.
I want to know if there is a way to group those rows by customized_id field.
By the way I tried Group by but it doesn't work

If you want group you need a aggregation function eg (max, min group_concat) and group by , if you want all the values related to a customized_id on a row you could use group_concat eg:
SELECT custom_values.customized_id
,group_concat(custom_values.value )
FROM projects
INNER JOIN custom_values ON projects.id=custom_values.customized_id
GROUP BY custom_values.customized_id
order by custom_values.customized_id asc

Related

I want both distinct and nondistinct records in join mysql

SELECT distinct referrals.listing_id,submissions.listing_name
FROM submissions
INNER JOIN referrals ON referrals.listing_id=submissions.listing_id;
if i do
SELECT distinct referrals.listing_id,submissions.listing_name,referrals.timestamp
FROM submissions
INNER JOIN referrals ON referrals.listing_id=submissions.listing_id;
it gives me many others records without distinct listing_id
i want distinct listing_id with their timestamps(which are not distinct but are according to distinct listing_id)
Use aggregation, if you want one row per listing. For instance:
SELECT r.listing_id, s.listing_name, max(r.timestamp) as most_recent_timestamp
FROM submissions s INNER JOIN
referrals r
ON r.listing_id = s.listing_id
GROUP BY r.listing_id, s.listing_name;
SELECT DISTINCT applies to all the expressions in the SELECT.

MySQL - Group orders count by clients numbers

I want to group order's count to show how many clients have that number of orders.
I have come up with:
select count(*) as quantidade_pedidos, clientes.id
from pedidos
inner join clientes
on pedidos.cliente_id = clientes.id
where pedidos.aprovado = 1
group by quantidade_pedidos
but I just can't group by 'quantidade_pedidos' anyway.
Is there any way to group by a temporary column? Another way of doing this? show how many clients (number) have that number of orders placed?
Example
8 orders placed -> 3 clients have 8 orders placed
etc
Your original query is wrong. You need to group by clientes.id:
select count(*) as quantidade_pedidos, c.id
from pedidos p inner join
clientes c
on p.cliente_id = c.id
where p.aprovado = 1
group by c.id;
In an aggregation query, the unaggregated columns go in the group by, not the aggregated ones.
Also note that table aliases make the query easier to write and to read.
As for the question in the first line, use a subquery:
select quantidade_pedidos, count(*)
from (select count(*) as quantidade_pedidos, c.id
from pedidos p inner join
clientes c
on p.cliente_id = c.id
where p.aprovado = 1
group by c.id
) x
group by quantidade_pedidos;
But given that the query in the question doesn't work, I'm not sure this is what you really want to do.

MySQL GROUP BY not working

I'm trying to count the number of softwares names using mysql
Here is my original that show everything
Select dico_soft.FORMATTED,dico_soft.EXTRACTED,softwares.NAME
From dico_soft
INNER JOIN softwares
ON dico_soft.EXTRACTED=softwares.NAME;
Where is what I added count and Group by sql
Select dico_soft.FORMATTED,dico_soft.EXTRACTED,softwares.NAME,count(*)
From dico_soft
INNER JOIN softwares
ON dico_soft.EXTRACTED=softwares.NAME;
Group BY = softwares.NAME;
the result only give me 1 row which adds everything, the group by doesn't work
it should be
Group BY softwares.NAME;
and not
Group BY = softwares.NAME;
remove "=" from GROUP BY of SQL query
Select dico_soft.FORMATTED,dico_soft.EXTRACTED,softwares.NAME,count(*)
From dico_soft
INNER JOIN softwares
ON dico_soft.EXTRACTED=softwares.NAME;
Group BY softwares.NAME;
I think you need to execute the following query
Select softwares.NAME,count(softwares.NAME) as Quantity
From dico_soft
INNER JOIN softwares
ON dico_soft.EXTRACTED=softwares.NAME;
Group BY softwares.NAME;
This will fetch software name and the quantity of each one
You are trying set GROUP BY on JOIN table. You need grouping by FROM table. Also not need = in GROUP BY
Select dico_soft.FORMATTED,
dico_soft.EXTRACTED,
softwares.NAME,
count(dico_soft.EXTRACTED)
From dico_soft
INNER JOIN softwares ON dico_soft.EXTRACTED = softwares.NAME;
Group BY dico_soft.EXTRACTED;

inner-join mysql joining 3 tables

i am having problems using an inner join for 3 tables..
i need to display cust_id, the customer forename and surname, the product name<-(from products table) and date of sale<--(from sales table), also i need to display in order of the most recent dates first.
this is what i have got so far
enter SELECT
customers.cust_id,
customers.forename,
customers.surname,
products.prod_name,
sales.Date_of_sale
FROM
customers
INNER JOIN
sales
ON
customers.cust_id = sales.cust_id; here
id really appreciate it if you could help me here, thank you..
Just add one more JOIN to the products table and include an ORDER BY clause:
SELECT
c.cust_id,
c.forename,
c.surname,
p.prod_name,
s.Date_of_sale
FROM customers c
INNER JOIN sales s ON c.cust_id = s.cust_id
INNER JOIN products p ON s.product_id = p.product_id
ORDER BY s.Date_of_sale DESC
A Visual Explanation of SQL Joins
I think the problem is in your FROM parameter.
You specified only customer.
SELECT customers.cust_id, customers.forename, customers.surname, products.prod_name, sales.Date_of_sale
FROM
customers , products , sales
INNER JOIN
sales
ON
customers.cust_id = sales.cust_id;

MySql query to get count of days spent in each country for each purpose? (Get count of all record in second table present in first table)

I have three tables tl_log, tl_geo_countries,tl_purpose. I am trying to get the count of number of days spent in each country in table 'tl_log' for each purpose in table 'tl_purpose'.
I tried below mysql query
SELECT t.country_id AS countryID,t.reason_id AS reasonID,count(t.reason_id) AS
days,c.name AS country, p.purpose AS purpose
FROM `tl_log` AS t
LEFT JOIN tl_geo_countries AS c ON t.country_id=c.id
LEFT JOIN tl_purpose AS p ON t.reason_id=p.id
GROUP BY t.reason_id,t.country_id ORDER BY days DESC
But landed up with.
I am not able to get the count for purpose for each country in 'tl_log' that is not present in table 'tl_log'. Any help is greatly appreciated. Also, Please let me know if the question is difficult to understand.
Expected Output:
Below is the structure of these three tables
tl_log
tl_geo_countries
tl_purpose
If you want all possible combination of countries and purposes, even those that do not appear on the log table (these will be shown with a count of 0), you can do first a cartesian product of the two tables (a CROSS join) and then LEFT join to the log table:
SELECT
c.id AS countryID,
p.id AS reasonID,
COUNT(t.reason_id) AS days,
c.name AS country,
p.purpose AS purpose
FROM
tl_geo_countries AS c
CROSS JOIN
tl_purpose AS p
LEFT JOIN
tl_log AS t
ON t.country_id = c.id
AND t.reason_id = p.id
GROUP BY
p.id,
c.id
ORDER BY
days DESC ;
If you want the records for only the countries that are present in the log table (but still all possible reason/purposes), a slight modification is needed:
SELECT
c.id AS countryID,
p.id AS reasonID,
COUNT(t.reason_id) AS days,
c.name AS country,
p.purpose AS purpose
FROM
( SELECT DISTINCT
country_id
FROM
tl_log
) AS dc
JOIN
tl_geo_countries AS c
ON c.id = dc.country_id
CROSS JOIN
tl_purpose AS p
LEFT JOIN
tl_log AS t
ON t.country_id = c.id
AND t.reason_id = p.id
GROUP BY
p.id,
c.id
ORDER BY
days DESC ;
LEFT JOIN should be replaced by RIGHT JOIN