Multiple result select inside a main select? - mysql

I have a n-to-1 relation in MySQL tables.
Houses can have many owners.
I would like to list all owners for a house.
I have tried this:
SELECT id, name, address
,(
SELECT CONCAT(firstname)
FROM owners
WHERE houses_id = houses.id
) AS 'owner_firstname'
FROM houses;
but i get this results
Error Code: 1242
Subquery returns more than 1 row
I would like to have result like this
100 | Liberty House | 200, NY Street | me, myself, Iren

To address your error: you had a sub-query for your select that brought back multiple owners. The query needed to bring back one literal, in your case, the list of names. You can achieve this in MySQL using the GROUP_CONCAT() function to combine the names of owners for a particular house:
SELECT h.id, h.name, h.address, GROUP_CONCAT(o.firstname) AS owner_firstname
FROM houses AS h
INNER JOIN owners AS o ON h.id = o.houses_id
GROUP BY h.id, h.name, h.address

I think you're looking for group_concat:
select id, name, address,
(select group_concat(firstname separator ', ')
from owners where houses_id = houses.id) as 'owner_firstname'
from houses;

Related

SELECT statement across multiple tables

First off, I am not an SQL coder.
I am trying to select information from 2 different tables in the same database.
Table 1 is called trans and has 3 columns:
CustNumb, DoT, Amount
Table 2 is called cust and has multiple columns:
CustNumb, Name, Address, City, State, Zip, Phone
The SELECT statement that I'm trying to write will pull data from both tables.
SELECT trans.CustNumb
, cust.Name
, cust.City
, cust.State
, trans.DoT
, trans.Amount
, cust.Phone
,
FROM trans
, cust
WHERE CustNumb LIKE 1234
I THINK I need to use a JOIN statement, but I'm sure what kind of a JOIN or the proper syntax.
Thanks for your help!
You do need a JOIN. In fact, you should simply never write , in the FROM clause.
The JOIN you need would appear to be on the common column between the two tables:
SELECT t.CustNumb, c.Name, c.City, c.State, t.DoT, t.Amount, c.Phone
FROM trans t JOIN
cust c
ON t.CustNumb = c.CustNumb
WHERE c.CustNumb = 1234;
In addition, you should understand that LIKE is a string function and 1234 is not a string. Presumably, you just want equality -- so use =.

SQL Join return zerro, but should return a proper value

Thank you very much for previous help - How can I run my SQL, to get the result such as "John Doe, plumbing"
But still I have no luck with JOIN, here is a problem.
I have a tables called - skills & artist
Table skills has columns: id, cat_id and skill
Which will be:
26 52 Test
27 52 Test2
NB: 52 this is category_id, which is stored in table - category
Table artist has columns: name, skills
Which will be:
James [26,27]
Putin [26,27]
What I have done:
select name, skills from artist limit 10
It gives me a result, like
Putin [26,27]
Then I decided to see not ID's but real skills name, so I did
select s.skill, a.Name
from skills as s, artist as a
where s.id = a.skills
And was expecting, that I will see:
Putin [test1, test2]
But, instead of this I got
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0008 seconds.)
I tried a lot of different ways, can u say, why It is not working, and what I missed?
Join the tables, group by artist.Name and use group_concat():
select a.Name, concat('[', group_concat(s.skill), ']') as skills
from artist as a inner join skills as s
on replace(replace(a.skills, '[', ','), ']', ',') like concat('%,', s.id, ',%')
group by a.Name

Combine multiple rows and count into new columns in another table

create list of all writer and the count of books they have written .
*Create one new column as writers name adding the first and last name from the writers table
*Get the count of books they have written in a new coulmns as number of books written
*Finally List to be ordered by Writers name , title and launch date
Im have created a simple question here as im trying to build something on similar ground:
Books:
Writers:
Expected Result:
SELECT *
FROM books b
INNER JOIN (
SELECT CONCAT(first_name,' ', last_name) AS author,COUNT(*) As booksWritten
FROM books
GROUP BY CONCAT(first_name,' ', last_name) c
ON CONCAT(b.first_name,' ', b.last_name=c.author
Is this what you want?
In MySQL 8.x you can use a window function. For example:
select
b.book_id,
b.title,
b.launch_date,
concat(w.first_name, ' ', w.last_name) as writers_name,
count(*) over(partition by w.first_name, w.last_name) as book_count
from book_table b
join write_table w on w.book_id = b.book_id
order by b.launch_date

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 can I attack this mySQL query?

I need to combine two tables.
The first table is People(SSN PRIMARY KEY, fname, lname).
The other table is Cities(name, cityID), where name = "fname lname" i.e. 2 columns of People concatenated with a space between. One person may have multiple cities associated with them, and a city may have more than one person (many - many).
I want to combine the two into a table PeopleCities(SSN, cityID). If no person is found for a city, I need SSN to be 0 for that cityID. My experience is mostly with sqlite rather than mySQL, so I'm not very confident in my query.
Here is my query:
SELECT ISNULL(People.SSN, 0), Cities.cityID
FROM People
FULL OUTER JOIN Cities
ON (Cities.name = CONCAT_WS(" ", People.fname, " ", People.lname) FROM People);
You can use UNION ALL, first to get the people/city pairs that match and then to get the non-matches:
select p.ssn, coalesce(c.cityid, 0)
from people p left join
cities c
on c.name = concat_ws(' ', p.name, p.lname)
union all
select distinct 0, c.cityid
from cities c
where not exists (select 1 from people p where c.name = concat_ws(' ', p.name, p.lname);