Mysql Inner Join Issues - mysql

This gives me all my articles that contain both the mentioned entities.
SELECT COUNT(ArticlesEntity.article_id) AS article_count
FROM articles_entities ArticlesEntity
WHERE ArticlesEntity.entity_id IN ('ENTITY_ID_1','ENTITY_ID_2')
GROUP BY ArticlesEntity.article_id
HAVING article_count>=2
I now want to add something to this query that excludes any entity that has 'ENTITY_ID_3'.
I tried the following but it returned the same results:
SELECT COUNT(ArticlesEntity.article_id) AS article_count
FROM articles_entities ArticlesEntity
WHERE ArticlesEntity.entity_id IN ('ENTITY_ID_1','ENTITY_ID_2')
AND ArticlesEntity.entity_id NOT IN ('ENTITY_ID_3')
GROUP BY ArticlesEntity.article_id
HAVING article_count>=2
What am I missing / doing wrong?
Thanks in advance

Try something like:
SELECT COUNT(ArticlesEntity.article_id) AS article_count
FROM articles_entities ArticlesEntity
WHERE ArticlesEntity.entity_id IN ('ENTITY_ID_1','ENTITY_ID_2')
AND NOT EXISTS (
select 1
from articles_entities
where article_id = ArticlesEntity.article_id
and entity_id = 'ENTITY_ID_3')
GROUP BY ArticlesEntity.article_id
HAVING article_count>=2

There must be more to your query than what you're showing here, because from what you're showing in your question, the results should be identical for both queries (If entity_id is equal to either id_1 or id_2, it's already not equal to id_3).
Edit:
Sorry, just noticed two things.
1) in your problem statement you said the results are identical (as expected).
2) in your title, you say "Inner Join Issues", but... you ain't got no inner join.

Related

MySQL DISTINCT returning not so distinct results

Good day,
I have a small issue with MySQL Distinct.
Trying the following query in my system :
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`, `bookingcomment_message` FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791
The point is that there are bookings like 29791 that have many comments added.
Let's say 10. Then when running the above query I see 10 results instead of one.
And that's not the way DISTINCT supposes to work.
I simply want to know if there are any comments. If the comment ID is not 0 then there is a comment. Of course I can add COUNT(blabla) as comment_number but that's a whole different story. For me now I'd like just to have this syntax right.
You may try aggregating here, to find which bookings have at least a single comment associated with them:
SELECT
b.booking_id,
b.booking_ticket,
b.booking_price
FROM mysystem_booking b
LEFT JOIN mysystem_bookingcomment bc
ON b.booking_id = bc.bookingcomment_link
WHERE
b.booking_id = 29791
GROUP BY
b.booking_id
HAVING
COUNT(bc.bookingcomment_link) > 0;
Note that depending on your MySQL server mode, you might have to also add the booking_ticket and booking_price columns to the GROUP BY clause to get the above query to run.
You can try below - using a case when expression
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`,
case when `bookingcomment_message`<>'0' then 'No' else 'Yes' end as comments
FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791

Nested Select Statement Query with error "Incorrect syntax near the keyword 'GROUP' "

I have been through a few other posts relating to my error, but none of the solutions seem to work. I'm fairly new to SQL so sorry if its something really simple. I have two tables
Movie Inventory - which has columns movie_title, onhand_qty, and replacement_price
NotFlix - which has subscriber_name, queue_nbr, and movie_title
I am trying to join the two tables to output the total replacement price cost per customer, but when I do it gives me the error titled above. Here is my code, thanks in advance for any help!
SELECT subscriber_name, SUM (replacement_price) as replacement
FROM
(SELECT NotFlix.subscriber_name, NotFlix.movie_title, NotFlix.queue_nbr, MovieInventory.replacement_price
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
)
GROUP BY subscriber_name;
You are missing an alias:
SELECT AliasNameHere.subscriber_name, SUM (AliasNameHere.replacement_price) as replacement
FROM
(SELECT NotFlix.subscriber_name as subscriber_name, NotFlix.movie_title, NotFlix.queue_nbr, MovieInventory.replacement_price as replacement_price
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
) AliasNameHere
GROUP BY subscriber_name;
I Just don't get why are you doing a temporary table in FROM clause, you could just do a basic INNER JOIN here and potientialy avoid problem with alias name :
SELECT NotFlix.subscriber_name, SUM (MovieInventory.replacement_price) as replacement
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
GROUP BY subscriber_name;

How to use DISTINCT in WHERE CLAUSE to avoid repeated row returns

The result of this SQL query should return a set of 6 rows.
SELECT baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
FROM feedFinishes
LEFT OUTER JOIN baseFeeds
ON feedFinishes.GUID = baseFeeds.GUID
WHERE feedFinishes.nowDate = baseFeeds.nowDate
AND baseFeeds.siteURL LIKE '%www.example.com%'
Currently it returns 18 rows, the correct 6 rows are being repeated 3 times. I figure a solution to this is to have another WHERE clause something similar to:
WHERE DISTINCT feedFinishes.ID
After researching this I have tried:
SELECT baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
FROM feedFinishes
JOIN baseFeeds
ON feedFinishes.GUID = baseFeeds.GUID
WHERE baseFeeds.siteURL LIKE '%www.example.com%' AND feedFinishes.ID in
(SELECT ID FROM feedFinishes GROUP BY ID HAVING COUNT(ID)=1);
After the discussion found here
Alas, this still returns 18 rows. I believe the answer is similar to here
Any advice would be appreciated.
You should apply group by clause.
SELECT
baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
FROM feedFinishes
JOIN baseFeeds
ON feedFinishes.GUID = baseFeeds.GUID
WHERE baseFeeds.siteURL LIKE '%www.example.com%'
GROUP BY
baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted

MySQL: Subquery returns more than 1 row

I know this has been asked plenty times before, but I cant find an answer that is close to mine.
I have the following query:
SELECT c.cases_ID, c.cases_status, c.cases_title, ci.custinfo_FName, ci.custinfo_LName, c.cases_timestamp, o.organisation_name
FROM db_cases c, db_custinfo ci, db_organisation o
WHERE c.userInfo_ID = ci.userinfo_ID AND c.cases_status = '2'
AND organisation_name = (
SELECT organisation_name
FROM db_sites s, db_cases c
WHERE organisation_ID = '111'
)
AND s.sites_site_ID = c.sites_site_ID)
What I am trying to do is is get the cases, where the sites_site_ID which is defined in the cases, also appears in the db_sites sites table alongside its organisation_ID which I want to filter by as defined by "organisation_ID = '111'" but I am getting the response from MySQL as stated in the question.
I hope this makes sense, and I would appreciate any help on this one.
Thanks.
As the error states your subquery returns more then one row which it cannot do in this situation. If this is not expect results you really should investigate why this occurs. But if you know this will happen and want only the first result use LIMIT 1 to limit the results to one row.
SELECT organisation_name
FROM db_sites s, db_cases c
WHERE organisation_ID = '111'
LIMIT 1
Well the problem is, obviously, that your subquery returns more than one row which is invalid when using it as a scalar subquery such as with the = operator in the WHERE clause.
Instead you could do an inner join on the subquery which would filter your results to only rows that matched the ON clause. This will get you all rows that match, even if there is more than one returned in the subquery.
UPDATE:
You're likely getting more than one row from your subquery because you're doing a cross join on the db_sites and db_cases table. You're using the old-style join syntax and then not qualifying any predicate to join the tables on in the WHERE clause. Using this old style of joining tables is not recommended for this very reason. It would be better if you explicitly stated what kind of join it was and how the tables should be joined.
Good pages on joins:
http://dev.mysql.com/doc/refman/5.0/en/join.html (for the right syntax)
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html (for the differences between the types of joins)
I was battling this for an hour, and overcomplicated it completely. Sometimes a quick break and writing it out on an online forum can solve it for you ;)
Here is the query as it should be.
SELECT c.cases_ID, c.cases_status, c.cases_title, ci.custinfo_FName, ci.custinfo_LName, c.cases_timestamp, c.sites_site_ID
FROM db_cases c, db_custinfo ci, db_sites s
WHERE c.userInfo_ID = ci.userinfo_ID AND c.cases_status = '2' AND (s.organisation_ID = '111' AND s.sites_site_ID = c.sites_site_ID)
Let me re-write what you have post:
SELECT
c.cases_ID, c.cases_status, c.cases_title, ci.custinfo_FName, ci.custinfo_LName,
c.cases_timestamp, c.sites_site_ID
FROM
db_cases c
JOIN
db_custinfo ci ON c.userInfo_ID = ci.userinfo_ID and c.cases_status = '2'
JOIN
db_sites s ON s.sites_site_ID = c.sites_site_ID and s.organization_ID = 111

Select column or null?

Can I select like this?
SELECT DISTINCT idClient, idAcc,Description
FROM client, account
WHERE (account.idCliente = client.idCliente
OR account.idCliente is NULL )
Im getting trouble because it show to me duplicated results :x How can I do it ?
Thanks
EDIT:
RESULTS
idClient idAcc Description
1 3 good
1 2 bad
1 3 bad
Note that im getting 2 diferent Descriptions for same idAcc
EDIT2:
I realy need that search by NULL or Not NULL.
You are using an implicit join. Try using an explicit JOIN like so:
SELECT DISTINCT Description
FROM client
LEFT JOIN account ON account.idCliente = client.idCliente
I would rewrite your query to use a LEFT JOIN so that you get all situations where there is a client.idCliente, but also those where the account.idCliente doesn't exist.
Like so:
SELECT DISTINCT Description
FROM client
LEFT JOIN account ON client.idCliente = account.idCliente;