MariaDB query for parent and child category - mariadb

I have this MariaDB table "categories":
id category parent_id
1 electronics 0
2 fashion 0
3 TV 1
4 mobile 1
5 Sony 3
6 power bank 1
7 iphone 4
and I want result using MYSQL Query
1 electronics
3 electronics >> TV
5 electronics >> TV >> Sony
4 electronics >> Mobile
7 electronics >> Mobile >> iphone
6 electronics >> Power bank
2 fashion

WITH RECURSIVE
cte AS ( SELECT id, category, parent_id, category path
FROM categories
WHERE parent_id = 0
UNION ALL
SELECT c.id, c.category, c.parent_id, CONCAT(cte.path, ' > ', c.category)
FROM categories c
JOIN cte ON cte.id = c.parent_id )
SELECT * FROM cte;
https://dbfiddle.uk/?rdbms=mariadb_10.4&fiddle=d9d59ace66a91b6b95c14e31cd1f6d1b

Related

sql duplicates, while one value appers once in column 1, and once in column 2

Im trying to check how many times two teams have played against each other, while each appearing once at home and once away.
In the next table, we can see that Team 1 played against Team 2 three times, twice away and once at home, and Team 3 played against Team 4 twice, once away and once at home.
how can I do it using sqlite?
id_home
id_away
date
1
2
2/12
2
1
3/12
3
4
4/12
4
3
5/12
2
1
6/12
You can group by sorted pairings of the id_home and id_away teams:
with matches as (select distinct t1.id_home h, (select t2.id_away from teams t2 where t2.id_home = t1.id_home) a from teams t1), m1 as (select case when h < a then h else a end a1, case when a < h then h else a end a2 from matches)
select m1.a1 team1, m1.a2 team2, (select sum(m1.a1 = t3.id_home) from teams t3) home, (select sum(m1.a1 = t3.id_away) from teams t3) away from m1 group by m1.a1, m1.a2;
Output:
team1 | team2 | home | away
1 2 1 2
3 4 1 1

BigQuery subsselect example (count and sum)

In google BigQuery I have done a simple query to get how many music someone has listened.
What I need is to make a sum for all rows returned from the query below (some type of subquery)?
select count(1) cnt
from OF7.PETERV_TEST
where gender='F'
group by userId
Row f0_
1 14
2 1
3 7
4 18
5 1
6 4
7 2
8 2
expected result:
49
you can use:
SELECT sum(cnt)
FROM
(SELECT count(1) cnt
FROM OF7.PETERV_TEST
WHERE gender='F'
GROUP BY userId )

select statement order by top 3 from one column and the rest by another column

I need to use two different sortorders. I want to select top 3 based on column PopularitySortOrder ASC and the rest by SortOrder ASC.
Table
---------------------------------------------------------------------------------------
| Id | Product | Price | SortOrder | PopularitySortOrder |
---------------------------------------------------------------------------------------
1 Samsung Galaxy S6 Edge 100 1 2
2 iPhone 6 100 2 1
3 iPhone 5S 100 4 4
4 Samsung Galaxy S6 100 6 3
5 Google Nexus 6 100 3 5
6 Google Nexus 5 100 5 7
I've tried with following select, unfourtanly unsuccessful:
select *
FROM
(
select * from Temp1 t1 order by PopularitySortOrder LIMIT 3
union all
select * from Temp1 t2 where t2.Id <> t1.Id order by SortOrder
)
order by PopularitySortOrder, SortOrder asc
Expected result:
---------------------------------------------------------------------------------------
| Id | Product | Price | SortOrder | PopularitySortOrder |
---------------------------------------------------------------------------------------
2 iPhone 6 100 2 1 *
1 Samsung Galaxy S6 Edge 100 1 2 *
4 Samsung Galaxy S6 100 6 3 *
5 Google Nexus 6 100 3 * 5
3 iPhone 5S 100 4 * 4
6 Google Nexus 5 100 5 * 7
EDIT: Using SQLite version 3.7
Fiddle
t1 is not visible in the other part of the UNION query.
You can duplicate the popularity query, or use a temporary view, or (in SQLite 3.8.3 or later) a common table expression:
WITH MostPopular3 AS (
SELECT * FROM Temp
ORDER BY PopularitySortOrder
LIMIT 3
)
SELECT * FROM MostPopular3
UNION ALL
SELECT * FROM (
SELECT * FROM Temp
WHERE Id NOT IN (SELECT Id FROM MostPopular3)
ORDER BY SortOrder
)

Phonegap + SQLite Joining

I have two tables customer & home in a local database for a ipad application.
I have a SQL query
SELECT C.CustomerID AS 'CID', H.CustomerID AS 'HID', H.lotNumber AS 'lotNumber' FROM CustomerInformation C LEFT JOIN HomeInformation AS H ON ( H.CustomerID = C.CustomerID )
It returns the proper amount of records, and display my address data correctly. However, all the data in the HomeInformation table is null
CustomerID FirstName HomeID CusomterID lotNumber
1 Josh 1 1 73824
2 Smith 2 2 54353
3 Chris 3 3 75342
4 Bob 4 4 42342
5 John 5 5 24342
I have tried to use INNER join, Cross Join, and Outer Join, but no records are returned.

datasets and joins. ASP.net

I have 3 tables. Namely Job_Master,Print_details,Paper_Details.
The Structure of the table is as follows:
Job_Master:
jobno jobname amount
1 A 100
2 B 200
3 C 300
Print_Details id being the PK and jobno being FK
id jobno color
1 1 Cyan
2 1 Red
3 2 Black
4 3 Black
5 3 Green
Paper Details id being the PK and jobno being FK
id jobno Type
1 1 Art Paper
2 1 Photo Paper
3 2 Art Paper
4 3 Copier
5 3 Glossy Paper
I want a write a query in SQL server or perform Dataset operations in ASP.net so as to display the below resultset in the grid view:
Desired Resultset:
jobno jobname printDetails paperDetails amount
1 A CYAN,RED Art Paper,Photo Paper 100
2 B Black Art Paper 200
3 C Black,Green Copier,Glossy Paper 300
is this possible using dataset operations in ASP.net or in a SQL server query. I am using SQL server 2008 R2 as my database and the fromt end has been designed using ASP.net3.5.
Any help on this is much appreciated.Thanks in advance.
Try this
SELECT DISTINCT a.Jobno, a.jobname, COALESCE(b.color + ', ', '') + b.color,COALESCE(c.type + ', ', '') + c.type
FROM job_master a left JOIN cprint_details b ON a.jobno=b.jobno
left join paper_details c on a.jobno=c.jobno
ORDER BY a.jobno
Read here for more info LINK

Resources