Combine 2 Select queries with different conditions - mysql

i current have 2 Select queries at mySQL.
SELECT Provider Name, Country, GroupUpdateID FROM provider WHERE Country="Vietnam" AND Provider Name="Provider A"
SELECT GROUP_CONCAT(DISTINCT Country) AS result FROM provider WHERE GroupUpdateID="Group1"
How can i combine these 2 queries such that i am able to display the provider Name, Country, groupUpdateID and a new column named Result (which will contain country names which share the same GroupUpdateID)?
I am stuck because i have no idea on how to produce a single query since they both have different WHERE criteria.

Modify your second query such as to get the country list per GroupUpdateID (i.e. use GROUP BY):
SELECT
p.ProviderName,
p.Country,
p.GroupUpdateID,
c.result
FROM provider p
JOIN
(
SELECT GroupUpdateID, GROUP_CONCAT(DISTINCT Country) AS result
FROM provider
GROUP BY GroupUpdateID
) c ON c.GroupUpdateID = p.GroupUpdateID
WHERE p.Country = 'Vietnam'
AND p.ProviderName = 'Provider A';
Or use a correlated subquery in the SELECT clause:
SELECT
ProviderName,
Country,
GroupUpdateID,
(
SELECT GROUP_CONCAT(DISTINCT c.Country)
FROM provider c
WHERE c.GroupUpdateID = p.GroupUpdateID
) AS result
FROM provider p
WHERE Country = 'Vietnam'
AND ProviderName = 'Provider A';

Related

SQL Query, number of city and continent

I have a problem with a query.
I have to find for all the continent: the name of the continent, number of cities and number of countries. This is what I did
SELECT co.continent, COUNT(*)
FROM Country co
JOIN City c ON c.countrycode = co.code
GROUP BY co.continent
UNION
SELECT COUNT(*)
FROM Country co2
WHERE co.continent = co2.continent ( <---- ??? )
GROUP BY co2.continent
But I don't know if is it legal the part "WHERE co.continent = co2.continent" because the second query isn't a subquery of the first, is it? Is there another way to do this query?
UNION is not required, a single query with GROUP BY and COUNT aggregate will get the desired result, there could be multiple cities in the same country, a country could appear multiple times, use COUNT(DISTINCT...) to remove duplicates.
SELECT co.continent, COUNT(*) cities, COUNT(DISTINCT co.code) countries
FROM Country co
JOIN City c ON c.countrycode = co.code
GROUP BY co.continent
co.continent = co2.continent in the original union query is invalid. Queries in union are independent from each other.

Mysql join two tables based in id

I have two tables one is country another one state.Based on country id i need to group the state.I tried with joining but i couldn't get.How to get same table names in one columns and id also.
country example:
State example:
Expected Results
Thanks in advance.
You don't have to use join. Union with alias will work.
Here's your query:
SELECT r.name, r.type, r.parent_id, r.og
FROM (
SELECT c.name AS name, 'country' AS type, 0 AS parent_id, c.origin AS og
FROM country c
UNION
SELECT s.name AS name, 'state' AS type, s.country_id AS parent_id, s.country_id AS og
FROM STATE s
) as r
ORDER BY r.og, r.parent_id
Here, og is an extra projection created to match your sorting need.
select *
from country
join state
on state.country_id = country.origin
you can group by by just adding
group by country_id
As far as mixing the results from two queries, just use UNION ALL between the two queries. Easy.
SELECT query 1.
UNION ALL
SELECT query 2.

How to merge 2 separate query and group by specific field with group_concat

I have used UNION for 2 separate Mysql query, The query is as below.
select country,u_id from
(SELECT regionShortName as country , GROUP_CONCAT( urls_regions.u_id ) AS
u_id
FROM urls_regions
where LENGTH(regionShortName )<=2
GROUP by regionShortName)
UNION
(SELECT countries.name as country,
GROUP_CONCAT( urls_regions.u_id ) AS u_id
FROM `countries` countries
inner join regions on countries.regions_id = regions.id
INNER JOIN urls_regions ON regions.region = urls_regions.regionShortName
GROUP by countries.name, country)
The above query has given me the results as below,
I am not able to achieve as below,
Now I would like know how to group by the "country" by concat(merge or join) "u_id"
Is my query implemented is right approach or is there any other approach to achieve this.
Using below query I am able to get what I am expecting,
select tb.country, GROUP_CONCAT(tb.u_id, ',') from
((SELECT regionShortName as country , GROUP_CONCAT( urls_regions.u_id ) AS
u_id
FROM urls_regions
where LENGTH(regionShortName )<=2
GROUP by regionShortName)
union
(SELECT countries.name as country,
GROUP_CONCAT(urls_regions.u_id ) AS u_id
FROM `countries` countries
inner join regions on countries.regions_id = regions.id
INNER JOIN urls_regions ON regions.region = urls_regions.regionShortName
GROUP by countries.name, country)) tb group by tb.country
I have done 2 things
In my first line of query I have added GROUP_CONCAT(tb.u_id, ',')
In the last line added group by tb.country

Error on SQL JOIN on 2 nested COUNT and GROUPBY statements

I am trying to combine 2 queries, one counting the users in each country and the other counting the users in each country using public phones and merging the columns together. However, I keep getting an error near the inner join code region. I am using HeidiSQL so that is the only error message I get. Each nested query runs perfectly on its own. Anyone knows what's wrong?
SELECT
AllUsers.country,
AllUsers.TotalUsers,
PublicUsers.PublicPhoneUsers
FROM
(SELECT
country,
count(country) As "TotalUsers"
FROM
userlist
GROUP BY
country) AS "AllUsers"
INNER JOIN
(SELECT
country,
count(country) As "PublicPhoneUsers"
FROM
userlist
WHERE
phone_public = 1
GROUP BY
country) AS "PublicUsers"
ON
AllUsers.country = PublicUsers.country;
Please remove the double quotation marks from both your column and table aliases:
SELECT AllUsers.country, AllUsers.TotalUsers, PublicUsers.PublicPhoneUsers
FROM
(
SELECT country, COUNT(country) As TotalUsers
FROM userlist
GROUP BY country
) AS AllUsers
INNER JOIN
(
SELECT country, COUNT(country) As PublicPhoneUsers
FROM userlist
WHERE phone_public = 1
GROUP BY country
) AS PublicUsers
ON AllUsers.country = PublicUsers.country

MySQL nested counts and returning values

I have searched high and low and can't seem to get a way to do what I want. I have a table with some customers, some products and their relationships.
I want to count the amount of returned rows from this part of the query
SELECT id
FROM customer
WHERE customer.name = 'SMITH'
OR customer.name = 'JONES'
I also want to return the ids that match SMITH and JONES (or other customer names chosen). I want to use the count of the returned rows as a variable (denoted as #var). I only want to return the products, id, and count that match my variable.
My questions are:
Is there a way that this can be done in a single SQL query?
Is there a way to return the count as well as the values?
I don't want to have to throw this into a PHP script or the like.
SELECT x.pId, p.productdesc, count(x.dId) as count
FROM
(
SELECT DISTINCT cId, pId
FROM Client
WHERE cId IN
(
SELECT id
FROM customer
WHERE customer.name = 'SMITH'
OR customer.name = 'JONES'
)
)x
JOIN Products p ON x.pId = p.id
GROUP BY x.pId
HAVING count = #var
Thanks,
M
This is sort of a 'literal' answer to what your asked, as you can use subqueries in the having clause. However, with more information (sample data and expected result) there may be a better way of doing what you want.
select x.pid, p.productdesc, count(x.pid) as count
from (select distinct cl.cid, cl.pid
from client cl
join customer cu
on cl.cid = cu.id
where cu.name in ('SMITH', 'JONES')) x
join products p
on x.pid = p.id
group by x.pid, p.productdesc
having count(x.pid) = (select count(*)
from customer
where name in ('SMITH', 'JONES'))