I have three tables
table persons
id | name
table files
id | person_data | person_id
table items
id | item_name | file_id
Each person has multiple files and each file has multiple items.
Is there any way to select for each person all it's items using just mysql returning something like this:
person - file_id -> item 1, file_id -> item 2, file_id -> item 3
I was thinking on some type of JOIN, i tried all combinations that i could imagine but i think that join it's not a solution, or...
Use GROUP_CONCAT.Please check SQLFiddle
SELECT
b.id,b.name,
GROUP_CONCAT(b.items SEPARATOR '|')
FROM (SELECT
p.id,p.name,
CONCAT(f.id,' -> ', GROUP_CONCAT(i.item_name)) AS items
FROM persons p,
files f,
items i
WHERE p.id = f.person_id
AND f.id = i.file_id
GROUP BY f.id) AS b
GROUP BY b.id
SELECT ti.id, ti.item_name
FROM TableItems ti
INNER JOIN TableFiles tf ON tf.id = ti.file_id
INNER JOIN TablePersons tp ON tp.id = tf.person_id
WHERE tp.id = :id
Where :id is the id of the person you want to list items for.
Related
I looked and tried various examples, but still not get the results I need.
With have a table called "item" which has metadata about media (books, vidoes, mp3 ,etc"
and a table "item_format" with the URL of the video
and Table with "categories"
and a relational to called "categories_item"
I want to find all the record on our youTube videos sorted by categories. so that if you look for "parenting" we get list a list a youTube and the title.
parenting https://youtube/v/K8cJKirMo-A Love is The Tey
https://youtube/v/uodqINpEC_w Discipline
https://youtube/v/Ko-ZboCzR64 Become Her Friend
and so on, listing all the categories assigned
item.media-type LIKE '%video%'
item
===============
item_id media_type title
1 video/teachings Discipline
2 video/news December Update
3 video/landscape Quad Copter Noni Field
item_format
=================
item_format_id item_id format
1 2 https://youtube/v/K8cJKirMo-A
2 4 https://youtube/v/uodqINpEC_w
category
=================
category_id item_id name
1 2 parenting
2 4 ethics
category_item # the bridging table between item/category
=================
category_item_id category_id item_id
1 2 2
2 4 3
I tried things like this but get errors, frankly out my depth here
select item.title, item_format.format
from item i
left join item_format if
on i.item_id = if.item_id
left join category_item ci
on i.item_id = ci.item_id
left join category c
on ci.category_id = category_id
where i.media_type LIKE '%video5'
order by c.name
I can't get to first base
Unknown column 'item.title' in 'field list' #line 1
your query is ok , but a small change need. you use table alias. in first line just use table alias instead of table name
select i.title, if.format,c.name
from item i
left join item_format if on (i.item_id = if.item_id)
left join category_item ci on (i.item_id = ci.item_id)
left join category c on (ci.category_id = c.category_id)
where i.media_type LIKE '%video5'
order by c.name
you have to provide alias once you have given it:
see if it works:
select i.title, if.format,c.name
from item i
inner join item_format if
on i.item_id = if.item_id
inner join category c
on i.item_id=c.item_id
inner join category_item ci
on c.category_id=ci.category_id
where i.media_type LIKE '%video%'
order by c.name
or this:
select i.title,if.format,c.name
from item i,item_format if,category c, category_item ci
where
i.item_id=if.item_id and
i.item_id=c.item_id and
c.category_id=ci.category_id
order by c.name;
I have a products database which has a multi-tier category structure. Products are assigned to a category. The category table looks like this:
id name parent_id
================================
1 Electronics NULL
2 AV 1
3 Speakers 2
4 Wireless 3
What I want to do is, as part of my SELECT statement for products, output a concatenated string of the category tree.
The product is always assigned to the last category, so for example, Product "500w Wireless Speakers" would be assigned to category_id 4 (based on the above).
The ouputted column should be Electronics-AV-Speakers-Wireless.
Is this possible to do? I have looked at GROUP_CONCAT() but I'm having trouble working out the correct syntax.
Join as many times as you need, and concat the names:
select concat(a.name, '-', b.name, '-', c.name, '-', d.name) name
from mytable a
join mytable b on a.id = b.parent_id
join mytable c on b.id = c.parent_id
join mytable d on c.id = d.parent_id;
I have two tables category and adverts, i need to select all categories and the number of adverts it has in the adverts table that has at least count greater than zero
the category table
cat_id | Name
----------------
1 | Toys
2 | fashion
3 | electronics
The adverts table
cat_id | title
----------------
1 | a
2 | b
2 | c
1 | d
2 | e
what i expect
cat_id | count | Name
-----------------------
1 |2 | a
2 |3 | b
The query i tried
Select
c.name, c.cat_id,c.parent_id, #count:= (Select Count(av.cat_id) From adsview av Where av.cat_id = c.cat_id)
from
category c WHERE #count > 0
i am getting and empty result, what am i doing wrong?
If you want to make sure that the cat_id from category table are in adverts table you need to join as
select
c.cat_id,
c.Name,
count(a.cat_id) as `count`
from category c
join adverts a on a.cat_id = c.cat_id
group by c.cat_id ;
select cat_id, count(*)
from adverts
group by cat_id;
So the mySQL query engine will grab every single row from the adverts table, it'll put them into neat piles where all rows in the pile have the same category, it'll count the number of rows in each pile, and then it'll return to you a result row for each pile with the pile's id and the number of rows.
Now lets add something: we want to also get the category's name. So we indicate that in the select clause, and add a join to the from clause. The join says "for every row in table a, consider it alongside every row in table b. if some condition holds, put this combined row into the from set". You can see that joins are actually quite slow in SQL (relatively).
select c.cat_id, count(*) as count, c.name
from adverts as a join categories as c on a.cat_id = c.cat_id
group by c.cat_id;
Note also that I've aliased the tables as a and c respectively, so as to remove the ambiguity over the column name cat_id (otherwise the mySQL query engine may get confused).
You can try this, mate:
SELECT
c.cat_id,
COUNT(a.cat_id) AS count,
a.title
FROM
category c
LEFT JOIN adverts a ON a.cat_id = c.cat_id
GROUP BY
c.cat_id
HAVING
count > 0;
or this:
SELECT
c.cat_id,
COUNT(a.cat_id) AS count,
a.title
FROM
category c
INNER JOIN adverts a ON a.cat_id = c.cat_id
GROUP BY
c.cat_id;
You have to use group by function like below
select cat_id, count(*) as count
from adverts
group by cat_id;
I have 2 tables in a database person and order tables.
PERSON table:
PERSON_ID | NAME
ORDER table:
ORDER_ID | ORDER_NO | PERSON_ID
I need to display all the orders + a name of corresponding person if it exists, if not just order details.
So far I got up to query:
SELECT ORDER_ID, ORDER_NO, order.PERSON_ID, NAME
FROM person, order
WHERE person.PERSON_ID = order.PERSON_ID AND
person.FIRST_NAME IS NOT NULL;
Which gives me orders only if the name is available whereas I need to display all the orders despite the fact if name is available or not.
Any suggestions?
Yes, you can use LEFT JOIN for that:
SELECT o.order_id, o.order_no, o.person_id, p.name
FROM `order` o
LEFT JOIN person p
ON p.person_id = o.person_id AND p.FIRST_NAME IS NOT NULL
With LEFT JOIN if the name is null it will still give you the orders.
I have three tables
item_to_user (to store the relations between user and item)
| item_to_user_id | user_id | item_id |
-----------------------------------------
item_tb
| item_id | item_name |
-----------------------
user_tb
| user_id | user_name |
-----------------------
An item can belong to one or more user and viceversa, that's why I'm using the first table.
So, given the user_id = A and user_id = B how can I do a mysql query to select all the items the belong both to user A and user B?
note: I wrote a similar question yesterday but was about two tables not three.
SELECT i.*
FROM item_tb AS i
LEFT JOIN item_to_user AS iu
ON iu.item_id = i.item_id
LEFT JOIN user_tb AS u
ON iu.user_id = u.user_id
WHERE u.user_name IN ('A', 'B')
GROUP BY i.item_id
HAVING COUNT(i.item_id) > 1
By prequerying common items between A and B (via count(*) = 2) will pre-limit the final list of items possible to get details from. Then joining that to the items table as SECOND table in the query should help performance. Especially if A&B have 50 common items, but your items table consists of 1000's of items.
select straight_join
i.item_id,
i.item_name
from
( select iu.item_id
from item_to_user iu
join user_tb u
on iu.user_id = u.user_id
and u.user_name in ( 'A', 'B' )
group by 1
having count(*) = 2 ) Matches,
item_tb i
where
Matches.item_id = i.item_id
If a user can't have repeated items then this simple one will work:
select item_id
from item_to_user
where user_id in ('A', 'B')
group by item_id
having count(*) > 1