i have 2 tables
[series]
--------------
ID | ART
--------------
1 | sculptor
2 | painter
3 | writer
-----------
[artists]
--------------
NAME | ART_IDs
--------------
john | 1
jack | 1,2
jill | 2,1
jeff | 3,1
which I want to join like this:
SELECT se.art, ar.name as artist
FROM series AS se
JOIN artists AS ar ON FIND_IN_SET(se.id , ar.art_ids) > 0
What I get is only the first values:
[result]
-------------------
ART | ARTISTS
-------------------
sculptor | john
sculptor | jack
painter | jill
writer | jeff
Instead of:
[result]
----------------------------
ART | ARTISTS
----------------------------
sculptor | john
sculptor,painter | jack
painter,sculptor | jill
writer,sculptor | jeff
Try this:
SELECT GROUP_CONCAT(se.art ORDER BY FIND_IN_SET(se.id , ar.art_ids)) as art, ar.name as artist
FROM series AS se
JOIN artists AS ar ON FIND_IN_SET(se.id , ar.art_ids) > 0
GROUP BY ar.name
If you want to concat column in each group, GROUP_CONCAT can help you.
Check demo in Rextester.
Related
I have 2 tables consisting of artists and tracks.
Artist
| id | name |
| -------- | -------------- |
| 1 | John Doe |
| 2 | Dave Wang |
Tracks
| id | artist_id | title |
| -------- | -------------- | -------------- |
| 1 | 1 | Song 1 |
| 2 | 1 | Song 2 |
I tried
SELECT a.name, b.title FROM Artist a, Tracks b WHERE a.id = b.artist_id
It returns all the songs of John Doe.
Is there a way to add Dave Wang on the result even it's just null on the title?
For example result
name
title
John Doe
Song 1
John Doe
Song 2
Dave Wang
null
Use an explicit left join:
SELECT a.name, b.title
FROM Artist a
LEFT JOIN Tracks b
ON a.id = b.artist_id;
As a side note, your current query is using the old school implicit join syntax. The version I gave above is the correct way of writing the join.
Please try this query
SELECT a.id, a.name,b.title FROM artist as a LEFT JOIN tracks as b on a.id = b.artist_id;
I have two independent tables: 'Clients' and 'Country'.
Country Table:
IdCountry Country
1 SPAIN
2 PORTUGAL
Clients Table
IdClient Entity IdCountry
1 Adam Alves 2
2 Peter Smith 2
3 David Ramos 1
4 Rafael Castro 1
I would like to add a new client into 'Clients' table but using the information from 'Country' table like this:
INSERT INTO Clients(IdClient, Entity, Country)
SELECT max(IdClient) + 1, '--New--' FROM Clients,
SELECT IdCountry FROM Country WHERE Country = 'SPAIN'
I would like to have this INPUT:
IdClient Entity IdCountry
5 --New-- 1
But if I run this query, it doesn't work. Could anybody help me, please?
COMMENTS: I prefer don't use autoincrement option.
Thank you very much.
Wardiam
You can do it like this:
INSERT INTO Clients(IdClient, Entity, Country)
SELECT
(SELECT MAX(IdClient) + 1 FROM Clients),
'--New--',
(SELECT IdCountry FROM Country WHERE Country = 'SPAIN')
See the demo.
Results:
| IdClient | Entity | Country |
| -------- | ------------- | ------- |
| 1 | Adam Alves | 2 |
| 2 | Peter Smith | 2 |
| 3 | David Ramos | 1 |
| 4 | Rafael Castro | 1 |
| 5 | --New-- | 1 |
I need help. I can't seem to find the logic behind this code.
I am working on a voting system, and I need to output the results of the votes.
I want to count all of the rows that has a unique name in it and output how many.
My table goes like this.
voterid | pres | vpres | sec | trea | PIO
---------------------------------------------
1 | John | Mitch | James | Jack | Eman
2 | John | Pao | Bryan | Jack | Faye
3 | Kelvin | Pao | James | Jeck | Faye
Output should be
Pres | Votes
--------------
John | 2
Kelvin | 1
Here's my code.
SELECT DISTINCT
pres,
(SELECT COUNT(pres) FROM (SELECT DISTINCT pres FROM tblVote AS Votes)) AS Votes
FROM tblVote
Thanks in advance!
I think you are just looking for a simple GROUP BY query:
SELECT pres, COUNT(*) AS Votes
FROM tblVote
GROUP BY pres
What I'm hoping to do is create a string out of a table WITHIN a query so that I may be able to place that string in another query I'm creating. Say, I have this for a table:
index | position | name
----------------------------------------
1 | member | John Smith
2 | chair | Mary Jones
3 | member | Mary Jones
4 | contact | Grace Adams
5 | director | Grace Adams
6 | member | Grace Adams
7 | treasurer | Bill McDonnell
8 | vice chair | Bill McDonnell
9 | member | Ishmael Rodriguez
I'm looking for the result as follows:
name | positions
----------------------------------------
John Smith | member
Mary Jones | chair,member
Grace Adams | contact,director,member
Bill McDonnell | treasurer,vice chair
Ishmael Rodriguez | member
I was hoping I could use some variant of CONCAT_WS() to get my result, like this...
SELECT
a.NAME,
CONCAT_WS(
',',
(
SELECT
position
FROM
TABLE
WHERE
NAME = a.NAME
)
)AS positions FROM ---------------
Obviously, this isn't working out for me. Any ideas?
Use GROUP_CONCAT[docs]
SELECT name, GROUP_CONCAT(position) result
FROM tableName
GROUP BY name
ORDER BY `index`
SQLFiddle Demo
Use GROUP_CONCAT like so:
SELECT name, GROUP_CONCAT(position SEPARATOR ',')
FROM Table
GROUP BY name
I have two tables
[series]
--------------
ID | ART
--------------
1 | sculptor
2 | painter
3 | writer
--------------
[artists]
--------------
NAME | ART_IDs
--------------
john | 1
jack | 1,2
jill | 2,1
jeff | 3,1
which I want to join like this:
SELECT se.art, ar.name as artist
FROM series AS se
INNER JOIN artists AS ar ON (se.id IN (ar.art_ids))
What I get is only the first values:
[result]
-------------------
ART | ARTISTS
-------------------
sculptor | john
sculptor | jack
painter | jill
writer | jeff
Instead of:
[result]
-------------------
ART | ARTISTS
-------------------
sculptor | john
sculptor | jack
sculptor | jill
sculptor | jeff
painter | jack
painter | jill
writer | jeff
Normally I would do this with a third table with the links pe.id<->se.id. But another table is quite complicated to maintain in my framework.
As mentioned above the best option is to fix your table structure, its best to do it now while you have the chance. As the data grows it will start causing a lot of headaches. However if you know what you are doing, i think this will get you what you want in the short term:
SELECT se.art, ar.name as artist
FROM series AS se
JOIN artists AS ar ON FIND_IN_SET(se.id , ar.art_ids) > 0