Scopes in MySQL & Derived tables - mysql

Let's say I have 2 Tables
One named Baskets,
Another named Fruits.
Baskets-
basket_id , basket_name
1 - Basket One
2 - Basket Two
Fruits-
fruit_id , basket_id , fruit_name
1 - 1 - Banana
2 - 1 - Apple
3 - 2 - Pear
SELECT * FROM baskets
JOIN (SELECT GROUP_CONCAT(fruit_id SEPARATOR ', ') FROM fruits WHERE baskets.basket_id=fruits.basket_id) AS der_fruits
ON baskets.basket_id=der_fruits.basket_id
Now with this query I want to get 2 rows (since there are 2 baskets) with a list of the fruit id's in it.
Like this:
basket_id, fruits
1 - 1, 2
2 - 3
But just now what I get is this:
basket_id, fruits
2 - 1, 2, 3
The thing is, I have to pass the global baskets.basket_id value in the DERIVED table.
Is there anything like a global scope in MySQL?
Or is there a way to pass the global baskets.basket_id value in a variable inside that derived table?

SELECT baskets.*,
(SELECT GROUP_CONCAT(fruits.fruit_name)
FROM fruits f
WHERE b.basket_id = f.basket_id) AS der_baskets
FROM baskets b
The fruits are a subquery. I don't understand why you define the relationship twice. Is there something you are trying to do I don't understand?

Related

Get unique data across certain column in multiple tables - sql

I know this is probably so odd to ask. But lets say I have 3 tables:
Table 1
ID
Name
1
Adam
2
David
3
Conor
Table 2
ID
Name
1
Adam
2
Derek
3
Niall
Table 3
ID
Name
1
Adam
2
David
3
John
Is there any way I can write a query to get the unique names across all 3 tables. So it would return "Adam, David, Conor, Derek, Niall, John"
Order doesn't matter
If it helps, all name values are related to a names table
yes , one way is to union them
select name from table1
union
select name from table2
union
select name from table3
union automatically removes duplicate cases

how to sum of a column with different values

I have a MySQL table which contains val_type column which have 3 type of values
id val_type company
1 rib 1
2 mod 2
3 rib 2
4 rib 3
5 mod 1
6 trop 1
$res= SELECT SUM(val_type) from tabl_name GROUP BY company;
with above query I get sum of all types in one
Result Required : Rib=3, mod=2 and trop=1
I want to get sum of all three types with one MySQL query. like how many rib,mod and trop.
Thanks
It sounds like you want to count all three types. You only need a basic GROUP BY query:
SELECT
val_type,
COUNT(*) AS cnt
FROM tabl_name
GROUP BY
val_type;

query the same field multiple times in the same query

I need to filter a table in mysql but can't get past the beginning.
The table has 2 fields:
ID_house house_feature
1 1
1 2
1 4
1 5
2 1
2 3
2 4
3 1
3 2
3 3
I need to filter this table using the following parameters:
house feature = 1
AND
house feature = 2
AND
house feature = 3
So that I get all houses with the requested feature.
I already tried to create something similar to this:
SELECT *
FROM houses
WHERE
house_feature = 1
AND
house_feature = 2
AND
house_feature = 3
But it doesn't work as I expected.
Is there a way to get this result with MySQL?
It seems that I acn filter the table using only the OR operator but this way I can't get the right result.
Thanks in advance for any help.
tony
You can do so ,by matching the distinct count of features per house ,so the house with exactly these 3 features will be returned
SELECT *
FROM t
WHERE
house_feature IN(1 ,2,3)
group by ID_house
having count(distinct house_feature) = 3
Demo

How to show multiple values of a single field in a single row in a single query?

Table 1:Domain Link Result
======================================================================
||Column1(words) ||Column2(links) ||Column3(frequency) ||
======================================================================
1 1 Any Number
2 1 Any Number
3 1 Any Number
4 1 Any Number
1 2 Any Number
2 2 Any Number
3 2 Any Number
4 2 Any Number
Table 2:Sub Link Result
======================================================================
||Column1(words) ||Column2(sublinks) ||Column3(frequency) ||
======================================================================
1 a Any Number
2 b Any Number
3 c Any Number
4 d Any Number
1 e Any Number
2 f Any Number
3 g Any Number
4 h Any Number
And so on.
In the above scenario user entered 4 words and 2 domain links. Now the frequency of 4 keywords is calculated on domain links as well sublinks and stored in separate tables as shown above. I want an aggregate result like below:
Table 3:Final Result
==================================================================================
||Column1(words) ||Column2(Domain links) ||Column3(Total frequency) ||
==================================================================================
Row1: 1 1 Total of frequency in both tables
2 for word "1"
----------------------------------------------------------------------------------
Row2: 2 1 Total of frequency in both tables
2 for word "2"
----------------------------------------------------------------------------------
Row3: 3 1 Total of frequency in both tables
2 for word "3"
----------------------------------------------------------------------------------
Row4: 4 1 Total of frequency in both tables
2 for word "4"
----------------------------------------------------------------------------------
I tried the following query in MySQL:
SELECT t.`keyword`, t.`link` SUM( t.`frequency` ) AS total
FROM (
SELECT `frequency`
FROM `domain_link_result`
WHERE `keyword` = 'national'
UNION ALL
SELECT `frequency`
FROM `sub_link_result`
WHERE `keyword` = 'national'
)t GROUP BY `keyword`
But in Column 2 of the final result I get only first link instead of two links for row 1. How can I get both links or any number of links entered by user in a single row ?
Words and Links have VARCHAR as type and frequency has INT type.
If you want to collapse several rows into one and still be able to see the information, you have to use GROUP_CONCAT
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
This outputs the collapsed values separated by commas, i.e.: a string. In your programming language you can split this string if you need individual values.
Your query would look somehow like this
SELECT keyword, GROUP_CONCAT(links), SUM(frequency)
FROM (subquery)
GROUP BY keyword
Which would output something like this:
==================================================================================
||Column1(words) ||Column2(Domain links) ||Column3(Total frequency) ||
==================================================================================
Row1: 1 1,2 sum of freq.
----------------------------------------------------------------------------------
Row2: 2 1,2 sum of freq.
----------------------------------------------------------------------------------
Row3: 3 1,2 sum of freq.
----------------------------------------------------------------------------------
Row4: 4 1,2 sum of freq.
EDIT: Extra help for your query
Your query looks a little bit confusing to me. Try with a JOIN approach:
SELECT domain_link_results.word AS word,
GROUP_CONCAT(domain_link_results.links) AS domain_links,
domain_link_results.frequency + sub_link_results.frequency AS total_frequency
FROM domain_link_results
INNER JOIN sub_link_results
ON domain_link_results.word = sub_link_results.word
WHERE domain_link_results.word = "national"
GROUP BY domain_link_results.word
On the other hand, it might be better to have all the links in the same table, and an extra field to determine if it's a domain link or a sublink. Without knowing more about your system it is hard to say.

MySql - order a query result in "some mode"

I have this data on a table :
id name field
0 marco attack
1 andrea defense
2 luca medium
3 ernesto defense
4 vittorio medium
5 manuele attack
i need to order as field. BUT, the priority list order (for my example) should be defense-medium-attack.
so it must return :
andrea, ernesto, luca, vittorio, marco, manuele.
How can do it? bye
You should store the fields in a separate table and give them a sort order. Then you can join to that table.
As well as allowing you to sort efficiently, it also makes the table structure more relational - which is good.
id field sort
1 defense 1
2 medium 2
3 attack 3
id name field
0 marco 3
1 andrea 1
2 luca 2
3 ernesto 1
4 vittorio 2
5 manuele 3
select p.name,
ps.field
from players p
join playersort ps
on p.field = ps.id
order by ps.sort
SELECT
X.id,
X.name,
X.field
FROM (
SELECT id,
name,
field,
CASE field WHEN 'defense' THEN 1
WHEN 'medium' THEN 2
WHEN 'attack' THEN 3
END AS SortValue
FROM MyTable) AS X
ORDER BY X.SortValue