I have two tables"shopinfo" and "rating" and i am trying to count review with Join but showing me wrong result
Here is my first table "shopInfo"
id shopOpen shopname
1 2 abc
2 1 xyz
3 2 dnu
4 2 rfy
Here is my table "rating"
id shopId rating review
1 2 3 Lorem Ipsum
2 2 4 Lorem Ipsum
3 4 5 Lorem Ipsum
4 2 1 Lorem Ipsum
And here is my code which showing me wrong result in review count (showing me 6 , should be 3),
Where i am wrong ?
SELECT si.shopOpen, COUNT(r.review) as reviewCount, AVG(r.rating) AS AvgRating
FROM shopInfo si
LEFT JOIN rating r ON r.shopId=si.id
WHERE si.shopId = '2'
you are missing group by. when you use aggregated functions like sum(), avg() you have to group by non-aggregated column. In your case it is si.shopOpen
Here is the fiddle link provided by #VBoka
SELECT
si.shopOpen,
COUNT(r.review) as reviewCount,
AVG(r.rating) AS AvgRating
FROM shopInfo si
JOIN rating r ON r.shopId=si.id
WHERE si.shopId = '2'
group by
si.shopOpen
Related
I want to fetch questions of $id(dynamic,passing as parameter) and want to whether this is
"ParentbusinessId" or "businessId"
table name "nps_ques"
id config_id question
1 1 Lorem Ipsum
2 1 Lorem Ipsum2
3 1 Lorem Ipsum3
Table "nps_config"
id parentBusinessId businessId
1 4580 NULL
2 0 2
3 4580 3
I tried with following code
$this->db->select('nq.id as ques_id,nq.config_id as formId,nq.question,nc.parentBusinessId,nc.businessId)
->from('nps_ques nq')
->join("nps_config nc", "nc.id=nq.config_id")
->where("nq.config_id", $id)
->order_by("nq.id", "ASC");
You can use CASE.
See example below:
SELECT
config.id AS config_id,
questions.question,
case when config.parent_business_id is not null then config.parent_business_id else config.business_id end as business
FROM
questions
JOIN
config ON questions.config_id = config.id
WHERE
config.id = 1;
I have a follow table like so:
id eventid user
1 1 ABC
2 4 XYZ
3 4 DEF
4 1 XYZ
5 1 DEF
And an event table like so:
eventid title desc
1 tuv Lorem Ipsum
2 yfc Lorem Ipsum
3 jhk Lorem Ipsum
4 lmn Lorem Ipsum
I want a query that would output the list of popular events based ordered by the highest number of followers of that event.
For ecample, eventid 1 has the highest number of followers so that will be listed first and second will be event with eventid=4
select t1.eventid,t1.count(t1.eventid) as count_followers
from table_name t1 inner join events_table t2
on t1.eventid=t2.eventid group by t1.eventid order by count_followers desc
I have two tables:
Table A: ID Cars Planes Num
1 3 5 2
2 8 44 1
3 7 23 6
4 6 2 7
Table B: ID Horses Dogs Cats Elefants Num
1 3 5 2 3 3
2 8 44 1 22 4
3 7 23 4 14 8
4 6 2 3 15 5
What I need to do: I need to get all results from both tables and sort them by the "Num" Column, where the "number" actually is unique for each result from both rows.
Is it even possible to "merge" those two tables and order them by "num" or should I just get each table separately ordered and do two loops checking always for the next num jumping between tables?
Thanks
you can merge them like that with UNION .
try this:
select num from(
select num from table1
union all
select num from table2
)t
order by num asc
DEMO HERE
EDIT:
select id ,Cars,Planes, Horses,Dogs,Cats,Elefants,num from(
select id ,Cars,Planes,'No horses' Horses,'No dogs' Dogs,'No cats' Cats,'No elefants' Elefants,num from table1
union all
select id,'No cars' Cars,'No planes' Planes,Horses,dogs,Cats,Elefants, num from table2
)t
order by num asc;
DEmo with other columns
SELECT NUM FROM TABLEA
UNION ALL
SELECT NUM FROM TABLEB
ORDER BY 1
I'm new here. I thought I could ask for some help here on my php-sql homework.
I've been trying to extract all the sports associated to a registered person using SQL.
There are three tables in my mySql.
personnes which stores the individuals
[id] [sexe] [etat_civil] [nom] [prenom]
1 Homme M. Smith Alex
2 Femme Mme Alisha Elektra
3 Femme Mll Lord Yves
loisirs which stores the types of sports or leisures
id nom
1 Sport
2 Concert
3 Jeux vidéo
4 Jeux société
5 Voyage
6 Cinéma
7 Lecture
8 Théâtre
9 Danse
10 Animaux
11 Randonnée
12 Shopping
personnes_loisirs which stores the foreign key of the individuals which associated foreign keys id.
[id] [fk_personnes] [fk_loisirs]
1 1 1
2 1 2
3 1 3
4 2 1
5 2 3
6 2 4
7 2 5
8 3 7
9 3 8
10 3 9
Basically, I've been successful to extract the sport of a user but only if there's only 1 sport associated to him. Where there are more than 1, I fail to get the rest of it.
Here's the SQL
select nom
from `loisirs`
where id in
(select fk_loisirs
from `personnes_loisirs`
where id in
(select id
from `personnes`
where sexe='Homme' AND nom='Smith' AND prenom='Alex'))
This returns me 'Sport' but not 'Sport, Concert and Jeux Vidéo';
I think I must use JOIN to be able to retrieve all the 'loisirs' associated to SmithAlex
But I'm not sure how.
Please help.
select l.nom
from personnes p
join personner_loisirs pl on p.id = pl.fk_personnes
join loisirs l on l.id = pl.fk_loisirs
where p.nom = 'Smith' and p.prenom='Alex'
Your subquery is not correct. You shouldn't say where id in... you should say where fk_personnes in...
In your original query you made an error when checking for the link between personnes_loisirs and personnes - you're returning the id from personnes in the final query but you should be using fk_personnes. Basically this is a very good demonstration of why fields just named id can be confusing...
In addition you should then measure that against another way of constructing the query e.g.
select l.nom
from personnes p
inner join personnes_loisirs pl on pl.[fk_personnes] = p.[id]
inner join loisirs l on l.[id] = pl.fk_loisirs
where p.sexe='Homme'
and p.nom='Smith'
and p.prenom='Alex'
Try this query : (not tested)
SELECT loisirs.*
FROM loisirs
INNER JOIN personnes_loisirs ON personnes_loisirs.fk_loisirs = loisirs.id
INNER JOIN personnes ON personnes_loisirs.fk_personnes = personnes.id
WHERE personnes.sexe='Homme'
AND personnes.nom='Smith'
AND personnes.prenom='Alex'
;
Following up on this question, there is problem with the standard approach or a recursive approach in the case below.
For instance, I want to return the page with the same parent_id and some pages have multiple row contents,
page table
page_id page_title parent_id
1 a 1
2 aa 1
3 ab 1
4 ac 1
content table
content_id content_text
1 text one
2 text two
3 text one aa
4 text two aa
5 text one ab
6 text one ac
content structure table
page_id content_id order_in_page
1 1 1
1 2 2
2 3 1
2 4 2
3 5 1
4 6 1
The standard approach,
SELECT
p.*,
c.*,
x.*
FROM pages AS p
LEFT JOIN pages_structures AS x
ON x.page_id = p.page_id
LEFT JOIN pages_contents AS c
ON c.content_id = x.content_id
WHERE p.parent_id = '1'
AND p.page_id != '1'
result (it lists the row as 4 items),
page_id page_title parent_id content_text order_in_page
2 aa 1 text one aa 1
2 aa 1 text two aa 2
3 ab 1 text one ab 1
4 ac 1 text one ac 1
As you can notice that there are two rows with page_id 2 and one row for each 3 and 4. How do you display the data into HTML with the standard approach like below (as suggested in one of the answer in the previous question)?
echo $page[0]['page_title'];
echo $page[0]['content_text'];
But with set-based one, I can do it with this,
SELECT
page_id,
page_title,
MAX(IF(order_in_page = 1, content_text, NULL)) AS content_1,
MAX(IF(order_in_page = 2, content_text, NULL)) AS content_2,
.
.
.
FROM
pages AS p LEFT JOIN
pages_structures AS x ON x.page_id = p.page_id LEFT JOIN
pages_contents AS c ON c.content_id = x.content_id
WHERE
p.parent_id = '1'
AND
p.page_id != '1'
GROUP BY page_id
in PHP,
foreach($items as $item)
{
echo '<li><h3>'.$item['page_title'].'</h3>';
echo '<p>'.$item['content_1'].'</p>';
echo '<p>'.$item['content_2'].'</p></li>';
}
the HTML (it lists the row as 3 items which is correct),
<li>
<h3>aa</h3>
<p>text one aa</p>
<p>text two aa</p>
</li>
<li>
<h3>ab</h3>
<p>text one ab</p>
<p></p>
</li>
<li>
<h3>ac</h3>
<p>text one ac</p>
<p></p>
</li>
Hope this makes sense!
with the standard approach you just keep track of the current page in a variable, and when the page changes, you roll to a new list item.