MYSQL Concatenate columns without grouping - mysql

I have a single table containing country codes, and language ids
+------+------+-------------+
| id | iso | language_id |
+------+------+-------------+
| 1 | US | 4 |
| 2 | IE | 1 |
| 3 | DE | 2 |
| 4 | SG | 1 |
| 5 | FR | 3 |
| 6 | UK | 1 |
| 7 | AT | 2 |
+------+------+-------------+
What I need is a MySQL statement that will return a result set containing EVERY ISO and a concatenated string of ids where the language id matches
So in the example above, I am looking to get
+------+------+----------+
| id | iso | id_group |
+------+------+----------+
| 1 | US | 4 |
| 2 | IE | 2,4,6 |
| 3 | DE | 3,7 |
| 4 | SG | 2,4,6 |
| 5 | FR | 5 |
| 6 | UK | 2,4,6 |
| 7 | AT | 3,7 |
+------+------+----------+
My best attempt so far is shown below and in the sqlfiddle link, but the grouping is excluding some of the ISO's. I need to return every row
SELECT iso, language_id, GROUP_CONCAT(id) as id
FROM countries
GROUP BY language_id
http://sqlfiddle.com/#!9/907618/3
Can this be done with MySQL or will I need to run many statements to get the results?
Thanks

This query will return all ID for every language ID:
select language_id, GROUP_CONCAT(id ORDER BY id) as id_group
from countries
group by language_id
then you just have to join this query with the countries table:
select
c.id,
c.iso,
g.id_group
from
countries c inner join (
select language_id, GROUP_CONCAT(id ORDER BY id) as id_group
from countries
group by language_id
) g on c.language_id = g.language_id
order by
c.id
Without a subquery you could use a self-join:
select
c.id,
c.iso,
group_concat(c1.id order by c1.id) as id_group
from
countries c inner join countries c1
on c.language_id = c1.language_id
group by
c.id,
c.iso

Related

query to get customer list using JOIN with sum () of the amounts spent in orders

I have the following tables
table anag (customer registry)
id | surname | name | phone |
----------------------------------------------
1 | Brown | Jack | +3989265781 |
2 | Smith | Bill | +3954872358 |
3 | Rogers | Stan | +3912568453 |
4 | Pickford | Eric | +3948521358 |
----------------------------------------------
table levels (table that connects each customer to his salesperson. For database registration reasons, the link between customer and seller is given by the customer's telephone number)
id | client_phone | id_seller |
--------------------------------------
1 | +3989265781 | 4 |
2 | +3954872358 | 7 |
3 | +3912568453 | 7 |
4 | +3948521358 | 8 |
--------------------------------------
table orders (contains all purchases made by customers, of course)
id | id_client | id_item | id_seller | price | status |
--------------------------------------------------------------------
1 | 1 | 2 | 4 | 12.50 | 2 |
2 | 2 | 2 | 7 | 12.50 | 2 |
3 | 2 | 3 | 7 | 10.00 | 3 |
4 | 2 | 3 | 7 | 10.00 | 3 |
5 | 2 | 4 | 7 | 20.50 | 1 |
6 | 3 | 2 | 7 | 12.50 | 1 |
7 | 3 | 5 | 7 | 19.00 | 3 |
8 | 3 | 7 | 7 | 31.00 | 2 |
9 | 4 | 1 | 8 | 5.00 | 1 |
--------------------------------------------------------------------
What I'm trying to do is get from the JOIN of these tables a complete list by seller of his customers sorted in descending order by the amount spent on orders as long as the order status is 2 or 3
Something like this (example seller id 7):
id | surname | name | amaount |
----------------------------------------
3 | Rogers | Stan | 50.00 |
2 | Smith | Bill | 32.50 |
----------------------------------------
I have tried with this query which seems correct to me, but unfortunately it returns me error in fetch_assoc()
SELECT a.id, a.surname, a.name, o.amount FROM levels AS l
JOIN anag AS a ON a.phone = l.client_phone
JOIN {
SELECT id_client, SUM(price) AS amount FROM orders
WHERE id_seller = '7' AND (status = '2' OR status = '3') GROUP BY id_client
} AS o ON o.id_client = a.id
WHERE l.id_seller = '7'
ORDER BY o.amount DESC
If I separate the subquery from the main query, both return the data I expect and it seems strange to me the JOIN between the two does not work properly
I think the only real error is using curly braces instead of parentheses:
SELECT a.id, a.surname, a.name, o.amount
FROM levels l JOIN
anag a
ON a.phone = l.client_phone JOIN
(SELECT id_client, SUM(price) AS amount
FROM orders
WHERE id_seller = '7' AND status IN ('2', '3'))
GROUP BY id_client
) o
ON o.id_client = a.id
WHERE l.id_seller = '7'
ORDER BY o.amount DESC;
In addition:
You can use IN to shorten an equality comparison to multiple values.
Although I left them in, status and id_seller look like numbers. If so, drop the single quotes. Don't mix data types.
Your question is ambiguous on what to do if the seller in orders differs from the seller in anag for a customer. This keeps your logic (the sellers need to match).
SELECT a.id, a.surname, a.name, sum(o.price) 'amount'
FROM anag a
LEFT JOIN levels l ON l.id =a.id
LEFT JOIN orders of ON o.id_seller = l.id_seller AND o.id_client = l.id
GROUP BY o.id_seller
ORDER BY amount DESC

Filter left join output

I have
jobs table with fields
jobId, jobTitle, jobDesc
jobQuotes Table with fields
id, user_id, quote
jobQuotes table has the quote of users who gave quote for the job.
I need those jobs for which a specific user has NOT given any quote.
Using LEFT JOIN I get all the jobs irrespective of the jobQuotes table.
And INNER JOIN only gives all the jobs that has a relevant jobQuote.
But I need those jobs for which a specific user has NOT given any quote.
My Query is
SELECT * FROM dummy_jobs J LEFT JOIN jobQuotes JQ ON J.jobId=JQ.jobId WHERE MATCH (J.jobTitle, J.jobDescription) AGAINST ('php, mysql');
How to filter this result set so that output doesn't have specific user_id in jobQuotes?
SELECT jobstable.jobid from jobstable inner join
(SELECT id from jobQuotes where userid = 953 and quote IS NULL) dummy_table
on dummy_table.id == jobstable.jobid;
The Answer is According to the comment you were given
"I want all jobs for which userId= 953 has not given any quote"
An approach might be to associate a specific user with all jobs by using a cross join and then left join to job quotes with a null test to find those not quoted for.
for example
users
+----+----------+
| id | username |
+----+----------+
| 1 | John |
| 2 | Jane |
| 3 | Ali |
| 6 | Bruce |
| 7 | Martha |
+----+----------+
Jobs
+-------+----------+---------+
| jobId | jobTitle | jobDesc |
+-------+----------+---------+
| 1 | a | a |
| 2 | b | b |
| 3 | c | c |
+-------+----------+---------+
Jobquotes
+------+---------+-------+
| id | user_id | quote |
+------+---------+-------+
| 1 | 3 | 10 |
| 2 | 2 | 10 |
+------+---------+-------+
select t.id,t.username,t.jobid,t.jobtitle,t.jobdesc
from
(
select u.id,u.username, s.jobid,s.jobtitle,s.jobdesc
from users u
cross join (select distinct jobid , jobtitle, jobdesc from jobs) s
where u.id = 3
) t
left join jobquotes jq on jq.id = t.jobid and jq.user_id = t.id
where jq.id is null
result
+----+----------+-------+----------+---------+
| id | username | jobid | jobtitle | jobdesc |
+----+----------+-------+----------+---------+
| 3 | Ali | 2 | b | b |
| 3 | Ali | 3 | c | c |
+----+----------+-------+----------+---------+

Select 10 records associated with each key

Here is the case I have two tables tags and customers as the following structure
Tags Table
ID Name
1 Tag1
2 Tag2
Customers Table
ID Tag_ID Name
1 1 C1
2 2 C2
3 1 C3
I want a SQL statement to get the first 10 customers (alphabetically) for each tag? is it possible to be done in one query.
P.S the data in the tables are sample data not the actual data
Consider the following:
DROP TABLE IF EXISTS tags;
CREATE TABLE tags
(tag_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name VARCHAR(12) NOT NULL
);
INSERT INTO tags VALUES
(1,'One'),
(2,'Two'),
(3,'Three'),
(4,'Four'),
(5,'Five'),
(6,'Six');
DROP TABLE IF EXISTS customers;
CREATE TABLE customers
(customer_id INT NOT NULL
,customer VARCHAR(12)
);
INSERT INTO customers VALUES
(1,'Dave'),
(2,'Ben'),
(3,'Charlie'),
(4,'Michael'),
(5,'Steve'),
(6,'Clive'),
(7,'Alice'),
(8,'Ken'),
(9,'Petra');
DROP TABLE IF EXISTS customer_tag;
CREATE TABLE customer_tag
(customer_id INT NOT NULL
,tag_ID INT NOT NULL
,PRIMARY KEY(customer_id,tag_id)
);
INSERT INTO customer_tag VALUES
(1,1),
(1,2),
(1,4),
(2,3),
(2,2),
(3,1),
(4,4),
(4,2),
(5,2),
(5,5),
(5,6),
(6,6);
The following query returns all customers associated with each tag, and their respective 'rank' when sorted alphabetically...
SELECT t.*, c1.*, COUNT(ct2.tag_id) rank
FROM tags t
JOIN customer_tag ct1
ON ct1.tag_id = t.tag_id
JOIN customers c1
ON c1.customer_id = ct1.customer_id
JOIN customer_tag ct2
ON ct2.tag_id = ct1.tag_id
JOIN customers c2
ON c2.customer_id = ct2.customer_id
AND c2.customer <= c1.customer
GROUP
BY t.tag_id, c1.customer_id
ORDER
BY t.tag_id,rank;
+--------+-------+-------------+----------+------+
| tag_id | name | customer_id | customer | rank |
+--------+-------+-------------+----------+------+
| 1 | One | 3 | Charlie | 1 |
| 1 | One | 1 | Dave | 2 |
| 2 | Two | 2 | Ben | 1 |
| 2 | Two | 1 | Dave | 2 |
| 2 | Two | 4 | Michael | 3 |
| 2 | Two | 5 | Steve | 4 |
| 3 | Three | 2 | Ben | 1 |
| 4 | Four | 1 | Dave | 1 |
| 4 | Four | 4 | Michael | 2 |
| 5 | Five | 5 | Steve | 1 |
| 6 | Six | 6 | Clive | 1 |
| 6 | Six | 5 | Steve | 2 |
+--------+-------+-------------+----------+------+
If we just want the top 2, say, for each tag, we can rewrite that as follows...
SELECT t.*
, c1.*
FROM tags t
JOIN customer_tag ct1
ON ct1.tag_id = t.tag_id
JOIN customers c1
ON c1.customer_id = ct1.customer_id
JOIN customer_tag ct2
ON ct2.tag_id = ct1.tag_id
JOIN customers c2
ON c2.customer_id = ct2.customer_id
AND c2.customer <= c1.customer
GROUP
BY t.tag_id, c1.customer_id
HAVING COUNT(ct2.tag_id) <=2
ORDER
BY t.tag_id, c1.customer;
+--------+-------+-------------+----------+
| tag_id | name | customer_id | customer |
+--------+-------+-------------+----------+
| 1 | One | 3 | Charlie |
| 1 | One | 1 | Dave |
| 2 | Two | 2 | Ben |
| 2 | Two | 1 | Dave |
| 3 | Three | 2 | Ben |
| 4 | Four | 1 | Dave |
| 4 | Four | 4 | Michael |
| 5 | Five | 5 | Steve |
| 6 | Six | 6 | Clive |
| 6 | Six | 5 | Steve |
+--------+-------+-------------+----------+
This is fine, but where performance is an issue, a solution like the following will be faster - although you may need to run SET NAMES utf8; prior to constructing the tables (as I had to) in order for it to work properly:
SELECT tag_id, name, customer_id,customer
FROM
(
SELECT t.*
, c.*
, CASE WHEN #prev=t.tag_id THEN #i:=#i+1 ELSE #i:=1 END rank
, #prev := t.tag_id
FROM tags t
JOIN customer_tag ct
ON ct.tag_id = t.tag_id
JOIN customers c
ON c.customer_id = ct.customer_id
JOIN ( SELECT #i:=1, #prev:=0) vars
ORDER
BY t.tag_id
, c.customer
) x
WHERE rank <=2
ORDER
BY tag_id,customer;
+--------+-------+-------------+----------+
| tag_id | name | customer_id | customer |
+--------+-------+-------------+----------+
| 1 | One | 3 | Charlie |
| 1 | One | 1 | Dave |
| 2 | Two | 2 | Ben |
| 2 | Two | 1 | Dave |
| 3 | Three | 2 | Ben |
| 4 | Four | 1 | Dave |
| 4 | Four | 4 | Michael |
| 5 | Five | 5 | Steve |
| 6 | Six | 6 | Clive |
| 6 | Six | 5 | Steve |
+--------+-------+-------------+----------+
To achieve this, we have to use two session variables, one for the row number and the other for storing the old customer ID to compare it with the current one as the following query:
select c.name, #row_number:=CASE
WHEN #cid = c.id THEN #row_number + 1
ELSE 1
END AS rows,
#id:=c.id as CustomerId from tags t, customers c where t.id=c.id group by c.name where Rows<=10
We used CASE statement in the query. If the customer number remains the same, we increase the row_number variable
Reference
Your question reminds me of this one (see especially the top-voted answer), so I came up with this query:
SELECT Tags.ID,
Tags.Name,
SUBSTRING_INDEX(GROUP_CONCAT(Customers.Name
ORDER BY Customers.Name),
',', 10) AS Customers
FROM Customers
INNER JOIN Tags
ON Tags.ID = Customers.Tag_ID
GROUP BY Tags.ID
ORDER BY Tags.Id;
It works, but this is clearly a hacky way to do this, because MySQL does not offer yet tools to do this more naturally.

How can I combine these two tables in SQL so that each row of the first has a copy of every row in the second?

I need to write an SQL query to combine two tables...
Table: names
+-------------+---------------+
| id | Name |
+-------------+---------------+
| 1 | Bob |
| 2 | Geoff |
| 3 | Jim |
+-------------+---------------+
Table: attributes
+-------------+---------------+
| id | Attribute |
+-------------+---------------+
| 1 | Age |
| 2 | Height |
| 3 | Weight |
+-------------+---------------+
...so that each record in the names table has a copy of each record in the Attributes table.
i.e.
+-------------+---------------+---------------+
| id | Name | Attribute |
+-------------+---------------+---------------+
| 1 | Bob | Age |
| 2 | Bob | Height |
| 3 | Bob | Weight |
| 4 | Geoff | Age |
| 5 | Geoff | Height |
| 6 | Geoff | Weight |
| 7 | Jim | Age |
| 8 | Jim | Height |
| 9 | Jim | Weight |
+-------------+---------------+---------------+
Is there a way to do this?
SELECT n.name, a.attribute from names n cross join attributes a
The same can be achieved with following query:
SELECT n.name, a.attribute from names n join attributes a ON 1=1
The only thing is that you show id from 1 to 9 in the output but there are no such IDs in the sample data. But if you have table with autoincrement then after you insert this data, the ID will be as you expected.
UPDATED:
As suggested in comments and if you need id, then you can do following:
SELECT (n.id-1)*3+a.id AS id, n.name, a.attribute from names n cross join attributes a
If you want to get an incremented id, just use a variable:
select (#rn := #rn + 1) as id, n.name, a.attribute
from names n cross join
attributes a cross join
(select #rn := 0) vars;
select stu.id,stu.name,attribute.attribute from stu cross join
attribute order by stu.id

Join top 3 interest fields along with each user row

I'm trying to get the top 3 interests of each user, probably as a LEFT JOIN query.
The way the app is designed, each user has a set of interests which are no other than 'childs' (rows without parent) of the categories table.
Here are some simplified table schemas w/mock data (see SQL Fiddle demo)
-- Users table
| ID | NAME |
--------------
| 1 | John |
| 2 | Mary |
| 3 | Chris |
-- Categories table -- Interests table
| ID | NAME | PARENT | | ID | USER_ID | CATEGORY_ID |
-------------------------------------- ------------------------------
| 1 | Web Development | (null) | | 1 | 1 | 1 |
| 2 | Mobile Apps | (null) | | 2 | 1 | 1 |
| 3 | Software Development | (null) | | 3 | 1 | 1 |
| 4 | Marketing & Sales | (null) | | 4 | 2 | 1 |
| 5 | Web Apps | 1 | | 5 | 2 | 1 |
| 6 | CSS | 1 | | 6 | 3 | 1 |
| 7 | iOS | 2 | | 7 | 3 | 1 |
| 8 | Streaming Media | 3 | | 8 | 3 | 1 |
| 9 | SEO | 4 |
| 10 | SEM | 4 |
To get the top 3 interests of a given user, I've usually performed this query:
SELECT `c`.`parent` as `category_id`
FROM `interests` `i` LEFT JOIN `categories` `c` ON `c`.`id` = `i`.`category_id`
WHERE `i`.`user_id` = '2'
GROUP BY `c`.`parent`
ORDER BY count(`c`.`parent`) DESC LIMIT 3
This query returns the top 3 categories (parents) of user with id = 2
I would like to find out how I can query the users table and get their top 3 categories either in 3 different fields (preferred) or as a group_concat(..) in one field
SELECT id, name, top_categories FROM users, (...) WHERE id IN ('1', '2', '3');
Any ideas how I should go about doing this?
Thanks!
First build a groped query that lists on distinct rows, the top three skills for each user. Then pivot that into to pull the three skills for eah user out to the right. You will need to use the Max(isnull(skill,'')) expression on the skills in each skill column.
It is very crude way of doing it in MYSQL to get top 3 records for each user
SELECT u.id, c.name
FROM
users u,
categories c,
(SELECT i.id,
i.user_id,
i.category_id,
#running:=if(#previous=i.user_id,#running,0) + 1 as rId,
#previous:=i.user_id
FROM
(SELECT * FROM intersect ORDER BY user_id) i JOIN
(SELECT #running=0, #previous=0 ) r) i
WHERE
u.id = i.USER_ID AND
i.CATEGORY_ID = c.id AND
i.rId <= 3
group by u.id, c.name ;
Hope it helps
FIDDLE