mysql table join problem - mysql

table: user
id name
-------------
1 john
2 paul
3 mattew
table: nickname
id user_id nickname
--------------------------
1 1 frog
2 1 cow
3 1 bull
4 2 cat
Result I want:
1 john frog cow bull
2 paul cat
3 mattew
How can I get this result?

Something like this should work:
SELECT u.id, u.name, GROUP_CONCAT(n.nickname, ' ') AS nickname
FROM user u
LEFT JOIN nickname n ON u.id = n.user_id
GROUP BY u.id, u.name
Please Note: syntax for GROUP_CONCAT may not be perfect because I haven't used it in a while

Use mysql aggregate functions: Group concat will do the trick for you.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

Related

Sql like expresion result too new column

Hi everybody I need done help with this issue.
Example myDb tabele name people
ID name Sex
3 Adam Smith M
5 Sonia Kolan F
3 Donald Smith M
Select id, name from people where name LIKE ‚%Smith%’;
Result
Id Name
3 Adam Smith
3 Donald smith
My question is how to transform my result of col name to this view
Id name
3 Smith
3 Smith
I want too see in col name Expresion from like statement
I want too see in col name Expresion from like statement
Then just put that literal string in the select clause:
select id, 'Smith' name
from people
where name like '%Smith%'
You can join to the table a query that returns only 'Smith':
select p.id, t.name
from people p
inner join (select 'Smith' name) t
on p.name LIKE concat('%', t.name, '%');
See the demo.
Results:
> id | name
> -: | :----
> 3 | Smith
> 3 | Smith

MySQL Left join with count on second Table

I have looked through some of the other posts on this site and am not seeing exactly what im looking for so here goes.
Lets say i have 2 tables
juser
-----------------------------
userID firstName lastName
-----------------------------
1 billy bob
2 jezze belle
3 bobbie sue
and:
juserrel
---------------------------------------------
id userID relUserID state
---------------------------------------------
1 1 2 approved
2 2 1 retired
3 2 1 approved
4 3 2 approved
What i am trying to do is get a result set that shows each user info about each user in the juser table and adds a column called connections to the result set which shows how many "active" connections a particular user has to another user.
the result i expect based on the tables above is
resultSet
-----------------------------------------------
userID firstName lastName connections
-----------------------------------------------
1 billy bob 2
2 jezze belle 3
3 bobbie sue 1
The query I tried is as follows
select userID, firstName, lastName , coalesce(x.cnt,0) as connections
from juser
left outer join (select count(*) cnt from juserrel where juserrel.userID =
userID or juserrel.relatedUserID = userID and juserrel.state = 'approved')
x on userID = userID
The result set i get looks like this:
resultSet
-----------------------------------------------
userID firstName lastName connections
-----------------------------------------------
1 billy bob 4
2 jezze belle 4
3 bobbie sue 4
Help Please ;)
Try:
select u.userID, u.firstName, u.lastName,
count(case when ur.state = 'approved' then 1 end)
from juser u
inner join juserrel ur on u.userID = ur.userID or u.userID = ur.relUserID
group by u.userID, u.firstName, u.lastName
SQL Fiddle Example

Query with GROUP_CONCAT not working

I have 3 tables with the following structure:
**users**
id
first_name
last_name
**specialties**
specialty_id
specialty_name
**user_specialties**
user_id
specialty_id
Here is some sample data:
**users**
1 Bill Smith
2 Tom Jones
3 Jill Hayes
**specialties**
1 word
2 web
3 database
**user_specialties**
1 1
2 1
2 3
3 2
3 3
I need to query the data so the specialties are concatinated on one row like the below output
**Desired Result**
Bill Smith word
Tom Jones word,database
Jill Hayes web,database
I am using the following query
SELECT
users.first_name,
users.last_name,
GROUP_CONCAT(specialties.specialtyname)
FROM
users
LEFT JOIN user_specialties ON user_specialties.user_id = users.userid
RIGHT JOIN specialties ON user_specialties.specialty_id = specialties.specialty_id
It is not working...
You're missing a GROUP BY clause. Most likely it should be GROUP BY users.id, and it'd go AFTER the JOIN lines.
I just tested this query
SELECT first_name,last_name,group_concat(specialty_name)
FROM user_specialties map
INNER JOIN specialties skill on user.id = map.user_id
INNER JOIN users user ON skill.specialty_id = map.specialty_id
GROUP BY user.id
Cheers! :-)

MySQL three table join, find item's rating

I'm hoping this is an easy one for you gurus, but my SQL knowledge is failing me.
My sample dataset:
item
----
item_id item_name item_added
1 Apple <date_time>
2 Banana <date_time>
user
----
user_id user_name
1 Alice
2 Bob
3 Carol
rating
------
rating_id item_id user_id rating_value
1 1 1 3
2 1 2 4
3 1 3 5
4 2 1 5
5 2 2 2
I want to find out what rating all three users have given to a particular item. The output should include NULL for rating_value if the user hasn't rated the item. For example, if I have item_id 2, I'd like to get this output:
user_name item_name rating_value
Alice Banana 5
Bob Banana 2
Carol Banana NULL
I've tried all kinds of joins, but I just can't seem to figure this one out.
Many thanks in advance.
It looks like you want a cartesian product of user and item, which will then be joined with rating:
select user_name, item_name, rating_value
from user as u, item as i
left join rating as r
on r.user_id = u.user_id
and r.item_id = i.item_id
I haven't done any serious work with MySQL for 4.5 years, but this should do it.
Edit: Maybe MySQL requires AS for the table aliases.
select u.user_name, i.item_name, r.rating_value
from item i,user u
left join rating r
on r.user_id = u.user_id
and r.item_id = i.item_id
This should do the trick..

Join, Group By and Sum in MySQL

I have the following three tables in a MySQL database in order to give ratings to comments of users:
Users:
id name
-----------
1 Smith
2 Brown
Comments:
id user_id post_id comment
-----------------------------------
1 2 1 Test 1
2 1 1 Test 2
3 1 1 Test 3
Scores:
id user_id comment_id score
------------------------------------
1 1 1 1
Now I want to select all the comments for post_id = 1, plus the username and the sum of all the scores on that specific comment. At first it looks very simple, I came up with this query:
SELECT users.name, comments.comment, SUM(scores.score) AS score
FROM comments
LEFT JOIN users ON users.id = comments.user_id
LEFT JOIN scores ON scores.comment_id = comments.id
WHERE comments.post_id = 1
GROUP BY scores.comment_id
It seems to work, but when there isn't a score for a specific comment, the comment doesn't show up, because MySQL can't GROUP BY NULL, I guess. So, is there any way to include those unrated comments? Like this:
Query result:
name comment score
-------------------------
Brown Test 1 1
Smith Test 2 0
Smith Test 3 0
You could try grouping on comments.id instead perhaps?