i have a problem.
I'm running following query. I have only one record in my database but i'm getting 9 results.
SELECT c.id, c.rk
FROM cv AS c, employee AS e , cvCat AS cv_cat
WHERE c.status=1
AND c.empIDFK = e.id
AND cv_cat.categoryFK IN ( 17,18,19,38,39,40,41,44,45,46 )
AND cv_cat.cvFK = c.id
Can someone please let me know if they is any problem with this query. Why i'm getting 9 results rather then just 1 result.
This query should only display one record but its showing 9 results.
When you do
FROM cv AS c, employee AS e , cvCat AS cv_cat
You are doing an implicit join of the three tables. If you want to get distinct records you can add DISTINCT after your select:
SELECT DISTINCT c.id, c.rk
FROM cv AS c, employee AS e , cvCat AS cv_cat
WHERE c.status=1
AND c.empIDFK = e.id
AND cv_cat.categoryFK IN ( 17,18,19,38,39,40,41,44,45,46 )
AND cv_cat.cvFK = c.id
Use explicit joins:
SELECT c.id, c.rk
FROM cv c
INNER JOIN employee e ON e.id = c.empIDFK
INNER JOIN cvCat cv_cat ON cv_cat.cvFK = c.id
WHERE c.status = 1
AND cv_cat.categoryFK IN (17,18,19,38,39,40,41,44,45,46)
I think the 9 result row "issue" is based on your JOIN syntax, as that kind of implicit join will return all rows (of each table) as a joined result, since you are only pulling out c.id and c.rk it may look like MySQL sent the same result back 9 times.
Side Note: Implicit joins are being deprecated, using explicit joins, such as...
SELECT c.id, c.rk
FROM cv c
LEFT JOIN employee e ON c.empIDFK = e.id
LEFT JOIN cvCat cv_cat ON c.id = cv_cat.cvFK
WHERE...
Will help "future-proof" your code a bit and add a little more self-describing syntax to the whole query.
Related
Hello i'm learning sql and i have some issues with joins(which i have problems understanding them)
I have this issue
#1066 - Not unique table/alias: 'tbl_respuestas'
what the query supposed to do, is count how many people(general,ignore user) has answer 'x', in 'y' question of 'z' survey
SELECT COUNT(*) FROM tbl_respuestas
INNER JOIN tbl_encuesta_usuario ON tbl_encuesta_usuario.user_id = user.id
INNER JOIN tbl_encuesta ON tbl_encuesta.id = tbl_encuesta_usuario.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas ON tbl_encuesta_has_tbl_preguntas.tbl_encuesta_id = tbl_encuesta.id
INNER JOIN tbl_preguntas ON tbl_preguntas.id = tbl_encuesta_has_tbl_preguntas.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas ON tbl_preguntas_has_tbl_respuestas.tbl_preguntas_id = tbl_preguntas.id
INNER JOIN tbl_respuestas ON tbl_respuestas.id = tbl_preguntas_has_tbl_respuestas.tbl_respuestas_id
WHERE tbl_respuestas.respuesta = 2
line SELECT COUNT(*) FROM tbl_respuestas
and line INNER JOIN tbl_respuestas
does not makes sense, hence the error.
Unless it is what you want then you need to give then different name/alias like below:
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_respuestas r2
Also as a quick note you can rewrite the entire sql like below.
It is good practice to give your tables a name for shorter referencing and makes the sql look a little cleaner.
Also if both tables you are trying to join has the same column name then you can use the keyword USING instead of having to write that long line tbl_encuesta_usuario.user_id = user.id
Please be sure to put r and r2 in its prope place
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_encuesta_usuario u USING user_id
INNER JOIN tbl_encuesta e ON e.id = u.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas hp ON hp.tbl_encuesta_id = e.id
INNER JOIN tbl_preguntas p ON p.id = hp.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas hr ON hr.tbl_preguntas_id = p.id
INNER JOIN tbl_respuestas r2 ON r2.id = hr.tbl_respuestas_id
WHERE r.respuesta = 2
I am trying to get the below query to delete all rows by id however the query is running fine but its not executing - no errors or rows effected. I have tripple checked all of the coloumn names in the tables and they are correct.
MYSQL:
DELETE c FROM campaigns c
JOIN campaignsFroms f ON f.campaign_id = c.id
JOIN campaignsRaw r ON r.campaignId = c.id
JOIN campaignsSubjects s ON s.campaign_id = c.id
WHERE c.id = 1582
I wonder if you want this:
DELETE c, f, r, s
FROM campaigns c LEFT JOIN
campaignsFroms f
ON f.campaign_id = c.id LEFT JOIN
campaignsRaw r
ON r.campaignId = c.id LEFT JOIN
campaignsSubjects s
ON s.campaign_id = c.id
WHERE c.id = 1582;
This will delete records from all the tables that are connected to the campaign. The LEFT JOIN is to ensure that the rows are not removed from consideration by the joins.
I wish to join multiple tables like- Categories, menus, restaurants, reviews, etc.
to return the restaurants that provide the inserted food with their prices.
Everything works except numberOfReviews in reviews table.
If a restaurant has no reviews then output should be 0 for numOfReviews column but other column values should be retrieved i.e. price, name, etc.
With following query I get all fields as null and count(numReviews) as 0:
select r.id
,r.`Name`
,r.`Address`
,r.city
,r.`Rating`
,r.`Latitude`
,a.`AreaName`
,m.`Price`
,count(rv.id)
from `categories` c, `menus` m, `restaurants` r, areas a, reviews rv
where m.`ItemName`="tiramisu"
and c.`restaurant_id`=r.`id`
and m.`category_id`=c.id
and r.`AreaId`=a.`AreaId`
and if I can't match rv.restaurant_id=r.id in where clause(obviously).
Where am I getting wrong? How do I solve this?
edited
select r.id,
r.`Name`,
r.`Address`,
r.city,
r.`Rating`,
r.`Latitude`,
a.`AreaName`,
m.`Price`,
r.`Longitude`,
r.Veg_NonVeg,
count(rv.id)
from restaurants r LEFT JOIN `reviews` rv on rv.`restaurant_id`=r.`id`
inner join `categories` c on c.`restaurant_id` = r.id
inner join `menus` m on m.`category_id` = c.id
inner join `areas` a on a.`AreaId` = r.`AreaId`
where m.`ItemName`="tiramisu"
First of all, don't use this old school syntax for the jointures.
Here is a query that may solve your problem:
SELECT R.id
,R.Name
,R.Address
,R.city
,R.Rating
,R.Latitude
,R.Longitude
,A.AreaName
,M.Price
,R.Veg_NonVeg
,COUNT(RV.id) AS numOfReviews
FROM restaurants R
INNER JOIN categories C ON C.restaurant_id = R.id
INNER JOIN menus M ON M.category_id = C.id
INNER JOIN areas A ON A.AreaId = R.AreaId
LEFT JOIN reviews RV ON RV.restaurant_id = R.id
WHERE M.ItemName = 'tiramisu'
GROUP BY R.id, R.Name, R.Address, R.city, R.Rating, R.Latitude, R.Longitude, A.AreaName, M.Price, R.Veg_NonVeg
I used explicit INNER JOIN syntax instead of your old school syntax and I modified the jointure with table reviews in order to get the expected result. The GROUP BY clause is required to use the aggregate function COUNT, every rows will be grouped by the enumerated columns (every column except the one used by the function).
Here is another solution that simplify the GROUP BY clause and allow the modification of SELECT statement without having to worry about the fact that every columns need to be part of the GROUP BY clause:
SELECT R.id
,R.Name
,R.Address
,R.city
,R.Rating
,R.Latitude
,R.Longitude
,A.AreaName
,M.Price
,R.Veg_NonVeg
,NR.numOfReviews
FROM restaurants R
INNER JOIN (SELECT R2.id
,COUNT(RV.id) AS numOfReviews
FROM restaurants R2
LEFT OUTER JOIN reviews RV ON RV.restaurant_id = R2.id
GROUP BY R2.id) NR ON NR.id = R.id
INNER JOIN categories C ON C.restaurant_id = R.id
INNER JOIN menus M ON M.category_id = C.id
INNER JOIN areas A ON A.AreaId = R.AreaId
WHERE M.ItemName = 'tiramisu'
As you can see here I added a new jointure on a simple subquery that does the aggregation job in order to provide me the expected number of reviews for each restaurant.
Hope this will help you.
I have a problem with mysql query. I am sending newsletters.
My mysql schema looks like to follwing http://sqlfiddle.com/#!2/178b7/18
How am I suppose to get from the database the COUNT of the mails that will be send.
For every List_ID in LISTS TAKE every base_id from lists_mailbases, and then count all of the base_id that are in email_database.
For example in my schema list1 should have 4 recipients, list 2 should have 2 recipients.
I wrote something like this:
SELECT l.id, COUNT(*) FROM lists l
JOIN lists_mailbases lm ON l.id = lm.list_id
JOIN email_database ed ON ed.base_id = lm.base_id
GROUP BY l.id;
but as a result, I have almost twice more results i should have. (on sqlfiddle it is ok but in my main database where i have a lot of records it gives me wrong data)
As you said, you are getting twice as much as you expect, it looks like one of the joins produces more than one record for the join. Diagnose that out.
SELECT ID, COUNT(*)
FROM
(
SELECT l.id, ED.BASE_ID FROM lists l
JOIN lists_mailbases lm ON l.id = lm.list_id
JOIN email_database ed ON ed.base_id = lm.base_id
GROUP BY l.id, ED.BASE_ID
) A;
nice question with SQLFiddle!
just add ed.base_id to your GROUP BY
SELECT l.id,ed.base_id, COUNT(*) , GROUP_CONCAT(ed.email)
FROM lists l JOIN lists_mailbases lm ON l.id = lm.list_id
JOIN email_database ed ON ed.base_id = lm.base_id
GROUP BY l.id,ed.base_id;
your lists_mailbases has repeated data by the way, it has 2 rows with the same base_id.
if your data is correct and you want to count DISTINCT base_id, just do COUNT(DISTINCT base_id)
like below
SELECT l.id, COUNT(DISTINCT ed.base_id) , GROUP_CONCAT(ed.email)
FROM lists l JOIN lists_mailbases lm ON l.id = lm.list_id
JOIN email_database ed ON ed.base_id = lm.base_id
GROUP BY l.id;
This question already has answers here:
Alternative to Intersect in MySQL
(9 answers)
Closed 8 years ago.
Trying to see what video categories "me_id" and "you_id" have both watched with:
SELECT c.title, COUNT(*) AS popularity
FROM video v
JOIN user u ON v.user_id = u.id
JOIN v_cat vc ON c.id = vc.vid_id
JOIN cat c ON c.id = vc.cat_id
JOIN u_cat uc ON uc.cat_id = c.id
WHERE uc.user_id = '$me_id'
INTSERSECT
SELECT c.title, COUNT(*) AS popularity
FROM video v
JOIN user u ON v.user_id = u.id
JOIN v_cat vc ON c.id = vc.vid_id
JOIN cat c ON c.id = vc.cat_id
JOIN u_cat uc ON uc.cat_id = c.id
WHERE uc.user_id = '$you_id'
GROUP BY c.title
ORDER BY uc.id DESC LIMIT 0, 10
I am working with PHP/MYSQL any thoughts?
MySQL doesn't have INTERSECT.
JOIN JOIN , Is that valid syntax?
FROM video v JOIN JOIN v_cat vc ON c.id = vc.vid_id
in the above line u have to use join one time..
I see two consecutive JOIN keywords in your first select but without any error message this is going to be difficult to debug.
Instead of INTERSECT, you can use the key word UNION or UNION ALL, this basically will do the intersection select. See http://dev.mysql.com/doc/refman/5.0/en/union.html