MySQL Query results AND - mysql

What I have is 3 tables: fruits, categories and fruit_category
fruits:
fruit_id | fruit_name
1 apple
2 orange
3 mango
4 banana
5 lanzones
6 pineapple
fruit_category:
fruit_id | cat_id
1 1
2 9
3 7
4 7
5 8
6 9
1 9
2 7
3 9
4 1
5 3
6 5
categories
cat_id | cat_name
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
When I execute a query looking for fruits that are either in categories 7 or 9 I get the correct results
fruit_name
apple
orange
mango
banana
pineapple
But when I use the AND operator, I always get 0 results. Below is my query for AND (same for OR)
SELECT `o`.`fruit_name`
FROM `fruits` AS `o`
JOIN `fruit_category` AS `oc`
ON `o`.`fruit_id` = `oc`.`fruit_id`
JOIN `categories` AS `c`
ON `c`.`cat_id` = `oc`.`cat_id`
WHERE `oc`.`cat_id` = 7
AND `oc`.`cat_id` = 9

You should use logical or (one value can't be equal to two unequal values at the same time), represented by in here:
where `oc`.`cat_id` in ( 7, 9)
Which is equivalent to:
where `oc`.`cat_id` = 7 or `oc`.`cat_id` = 9
If you want to ensure, that fruit_name is in both categories, you should use group by and having clauses after the where (that is represented above, with or):
group by `o`.`fruit_name`
having count(distinct `oc`.`cat_id`) = 2

SELECT
o.fruit_name
FROM
fruits as o
JOIN
fruit_category as oc
ON
o.fruit_id = oc.fruit_id
JOIN
categories as c
ON
c.cat_id = oc.cat_id
WHERE
oc.cat_id IN (7,9)
GROUP BY
o.fruit_name
HAVING
COUNT(oc.cat_id) = 2

Related

Mysql Joining four tables

I've been having trouble linking these tables together:
Table 1: Matches
ID
Name
Date
1
Adam vs Lance
2021-09-2021
2
Bex vs Adam vs Erica
2021-08-2021
3
Craig vs Bree
2021-07-2021
4
Danielle vs Alan
2021-06-2021
5
Erica vs Zoe vs AJ
2021-05-2021
6
Bree vs Erica
2021-04-2021
7
Bree vs Lance
2021-03-2021
8
Bree vs Lance vs Zoe
2021-02-2021
Table 2: Winners:
ID
Name
Match ID
IDNum
1
Adam
1
1
2
Bex
2
3
3
Danielle
4
7
4
Zoe
5
9
5
Erica
6
4
6
Bree
7
5
7
Bree
8
5
Table 3: Losers:
ID
Name
Match ID
IDNum
1
Lance
1
2
2
Adam
2
1
3
Erica
2
4
4
Alan
4
8
5
AJ
5
10
6
Erica
5
4
7
Bree
6
5
8
Lance
7
2
9
Lance
8
2
10
Zoe
8
9
Table 3: Draws:
ID
Name
Match ID
IDNum
1
Craig
3
6
2
Bree
3
5
Table 4: Players
ID
Name
Gender
1
Adam
M
2
Lance
M
3
Bex
F
4
Erica
F
5
Bree
F
6
Craig
M
7
Danielle
F
8
Alan
M
9
Zoe
F
10
AJ
F
The query I've been trying is to look up all matches with Bree in them and order them by date.
Table 5: Output:
Match ID
3
6
7
8
Draw: Match ID: 3
Los: Match ID: 6
Win: Match ID: 7
Win: Match ID: 8
When I try to inner join wins & losses against the Match table it works but the second I include the draws it does not return anything.
If I try just returning draws it works but then inner joining either losses or wins causes 0 results.
Can anyone help me with the code that'll work?
Query I'm trying:
SELECT Matches.ID AS MatchID, Winners.Name
FROM Matches
inner JOIN Draws
ON Matches.ID = Draws.MatchID
inner JOIN Winners
ON Matches.ID = Winners.MatchID
inner JOIN Losers
ON Matches.ID = Losers.Match ID
and (Winners.winner_id_num = 5
OR
Losers.type_id_num = 5
OR
Draws.IDNum = 5
)
GROUP BY match_id_num;
I would recommend you to use UNION between the 3 tables with results and join the output with the Matches table.
SELECT r.Result, r.IDMatch FROM (
SELECT *, 'Win' as Result FROM Winners WHERE IDNum = 5
UNION
SELECT *, 'Los' as Result FROM Losers WHERE IDNum = 5
UNION
SELECT *, 'Draw' as Result FROM Draws WHERE IDNum = 5
) AS r
INNER JOIN Matches AS m ON m.ID = r.IDMatch
ORDER BY m.Date DESC
The output would be:
Draw 3
Los 6
Win 7
Win 8

how to display result with order by based parent and childs in one table

I got problem with how to display query result based by parent and child on one table
data for example:
ID
CATEGORY
PARENT
ROOT
1
FOOD & DRINK
0
NULL
2
CLOTHES
0
NULL
3
FRUIT
1
1
4
DESERT
1
1
5
PANTS
2
2
6
T-SHIRT
2
2
7
APPLE
3
1
8
BANANA
3
1
9
ICE CREAM
4
1
10
SHORTS
5
2
11
LONG T-SHIRT
6
2
and I want to sort and display to this:
ID
CATEGORY
PARENT
ROOT
2
CLOTHES
0
NULL
5
PANTS
2
2
10
SHORTS
5
2
6
T-SHIRT
2
2
11
LONG T-SHIRT
6
2
1
FOOD & DRINK
0
NULL
4
DESERT
1
1
9
ICE CREAM
4
1
3
FRUIT
1
1
7
APPLE
3
1
8
BANANA
3
1
is there any query script to display like above ?
here my code and have no luck
select
id, category, parent, root
from
tbl_menu
order by
coalesce(root,parent,id),
category
and thanks for help :)
I understand that you want to order the tree nodes depth-first. One option uses a recursive query to generate the path to each node, that you can then use for ordering:
with recursive cte as (
select t.*, cast(id as char) as path from mytable t where root is null
union all
select t.*, concat(c.path, '/', t.id)
from cte c
inner join mytable t on t.parent = c.id
)
select *
from cte
order by path

MySQL get data with table joins

I have two tables. One is ps_product_lang and ps_category_product. The data inside ps_category_product is like this
id_category id_product
2 1
2 2
2 3
2 4
2 5
2 6
2 7
3 1
3 2
3 3
3 4
3 5
3 6
3 7
4 1
4 2
5 1
7 2
8 3
8 4
8 5
8 6
8 7
9 3
10 4
The table for ps_product_lang is like this
id_product id_lang name
1 1 Faded Short Sleeves T-shirt
1 2 Faded Short Sleeves T-shirt
2 1 Blouse
2 2 Blouse
3 1 Printed Dress
3 2 Printed Dress
4 1 Printed Dress
4 2 Printed Dress
5 1 Printed Summer Dress
5 2 Printed Summer Dress
6 1 Printed Summer Dress
6 2 Printed Summer Dress
7 1 Printed Chiffon Dress
7 2 Printed Chiffon Dress
So here I want to get the id_product,name from ps_product_lang where id_category is 5,7 and name like 'p%'. so can someone tell me what would be the query. Any help and suggestions would be really appreceable.
As #Gordon was hinting, you simply need an INNER JOIN between the two tables, along with a where condition to restrict the product category and name:
SELECT t1.id_product,
t1.name
FROM ps_product_lang t1
INNER JOIN
(
SELECT DISTINCT id_product
FROM ps_category_product
WHERE id_category IN (5, 7)
) t2
ON t1.id_product = t2.id_product
WHERE t1.name LIKE 'P%'
Here is a demo which omits the WHERE clause to show how the query behaves when it actually returns data:
SQLFiddle
Use JOIN to get data from 2 tables
SELECT p.id_product,name
FROM ps_product_lang p
JOIN ps_category_product c
ON p.id_product=c.id_product
WHERE id_category IN (5,7)
AND name LIKE 'p%'

mysql count and join

I want to retrieve table with total count from multiple table and joins
I have 4 wordpress custom database tables.
wp_tutorials
ID tut_name
1 php
2 mysql
3 wordpress
wp_chapters
ID tut_id chapter_name
1 1 php1
2 1 php2
3 2 mysql1
4 2 mysql2
wp_series
ID chapter_id series_name
1 1 php1A
2 1 php1B
3 2 php2A
4 2 php2B
5 3 mysql1A
6 3 mysql1B
7 4 mysql2A
8 4 mysql2B
wp_tut_users
ID series_id user_name
1 2 user1
2 2 user2
3 4 user3
4 6 user4
5 7 user5
from these four tables I want to retrieve by sql query following table.
1. tutorial
tut_name total_users
php 3
mysql 2
wordpress 0
expecting best ways...
Use a left join to get even tutorials with no users
select t.tut_name, count(u.id) as total_users
from wp_tutorials t
left join wp_chapters c on c.tut_id = t.id
left join wp_series s on s.chapter_id = c.id
left join wp_tut_users u on u.series_id = s.id
group by t.tut_name

Getting unique values as a query result

I have a many-to-many relation with my database, so I created a sql query which later I will use for search.
Now what I want to do is get a unique values from my search result (unique company names).
This is my query:
SELECT agencies.company
,agencies.website_url
,agencies.STATUS
,agencies.size
,IndustryData.industry_id
,ProfessionData.profession_id
,SectorData.sector_id
,seniorityData.seniority_id
,ZonesData.zone_id
FROM agencies
LEFT JOIN (
SELECT agencies_industries.agency_id
,agencies_industries.industry_id
FROM agencies_industries
) AS IndustryData ON agencies.id = IndustryData.agency_id
LEFT JOIN (
SELECT agencies_professions.agency_id
,agencies_professions.profession_id
FROM agencies_professions
) AS ProfessionData ON agencies.id = ProfessionData.agency_id
LEFT JOIN (
SELECT agencies_sectors.agency_id
,agencies_sectors.sector_id
FROM agencies_sectors
) AS SectorData ON agencies.id = SectorData.agency_id
LEFT JOIN (
SELECT agencies_seniorities.agency_id
,agencies_seniorities.seniority_id
FROM agencies_seniorities
) AS SeniorityData ON agencies.id = SeniorityData.agency_id
LEFT JOIN (
SELECT agencies_zones.agency_id
,agencies_zones.zone_id
FROM agencies_zones
) AS ZonesData ON agencies.id = ZonesData.agency_id
WHERE IndustryData.industry_id = 3
AND ProfessionData.profession_id = 1
The result looks like this:
company, website_url, status, size, industry_id, profession_id, sector_id, seniority_id, zone_id
Nine nine.com 1 3 3 1 3 2 1
Nine nine.com 1 3 3 1 3 2 5
Nine nine.com 1 3 3 1 3 2 8
Nine nine.com 1 3 3 1 3 5 1
Nine nine.com 1 3 3 1 3 5 5
Nine nine.com 1 3 3 1 3 5 8
Ten ten.com 2 3 3 1 3 1 1
Ten ten.com 2 3 3 1 3 1 3
Ten ten.com 2 3 3 1 3 1 7
Ten ten.com 2 3 3 1 3 3 1
Ten ten.com 2 3 3 1 3 3 3
Ten ten.com 2 3 3 1 3 3 7
Ten ten.com 2 3 3 1 3 5 1
Ten ten.com 2 3 3 1 3 5 3
Ten ten.com 2 3 3 1 3 5 7
I would like to get rid of the repeats of the company names. How do I do that?
You could use a GROUP BY for this, e.g.:
SELECT agencies.company
,agencies.website_url
,agencies.STATUS
,agencies.size
,IndustryData.industry_id
,ProfessionData.profession_id
,SectorData.sector_id
FROM ...
GROUP BY agencies.company
,agencies.website_url
,agencies.STATUS
,agencies.size
,IndustryData.industry_id
,ProfessionData.profession_id
,SectorData.sector_id
Maybe you could do that with variables and subqueries, as some examples shown here:
http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
But maybe doing this at your code is faster than in your query.