I have a little problem, have the following statement in mysql:
SELECT cmfilm.*, cmgenre.titel AS genretitel, cmvertoning.*, cmzaal.titel AS zaaltitel
FROM cmfilm
LEFT JOIN cmvertoning ON cmvertoning.film_id=cmfilm.id
LEFT JOIN cmgenre ON cmfilm.genre_id=cmgenre.id
LEFT JOIN cmzaal ON cmvertoning.zaal_id=cmzaal.id
WHERE cmvertoning.id IN(74,74,74,74)
ORDER BY cmfilm.id ASC
because i'm asking the same id 4 times it only gives me 1 result. But i would like to have the result to be returned 4 times, as i asked it to do. Anyone who knows how to solve this?
Maybe it would be better to achieve this on the application side - put returned rows in a map, then iterate over the ids you wanted and get appropriate rows from a map. Because there is another problem - the rows might not be in the order in which order you specified the ids (BTW. this row ordering problem can be solved also using field).
The UNION solution would work too. But, honestly, I think that you have some design problem if you need to fetch the same rows multiple times.
You did not ask for 4 rows, you asked for any rows where id is 74, 74, 74 or 74 - of which there is only one.
The real question is, why do you want the same row 4 times?
I don't think you can acomplish that without some sort of a loop, or by using the UNION ALL statement.
Related
Hello everyone i have a quick question, i am running mysql workbench and after joining two tables i get as results 10000 rows. Considering that the first dataset got 6000 rows and the second 450, it'clearly wrong. i'm clearly doing something wrong but i can't figure what is that and why it is happening
I am selecting some column from the first data set and match it against the second one against sv3 and sv4 columns
Can you tell me what i am doing wrong?
the code
select media.Timestamp, media.Campaign, media.Media, media.sv3, media.sv4
from media
inner join media_1
on media.sv3=media_1.sv3 and on media.sv4=media_1.sv4
JOIN queries yielding more results than their source records is not necessarily a sign something is outright wrong; but can be an indicator of something amiss (queries that need to behave that way exist, but are relatively rare).
The source of your issue is likely because you are joining on a value that is non-unique in both tables. As a simple example: If table X has two records with field A = 5, and table Y has three records with field A = 5, and they are JOINed on field A; those records will produce six results.
This may mean there is a problem with your source data, or you may just need to query it in a different manner. I notice you are only selecting fields from media and none from media_1; this query may yield the results you are expecting:
SELECT media.Timestamp, media.Campaign, media.Media, media.sv3, media.sv4
FROM media
WHERE (sv3, sv4) IN (SELECT sv3, sv4 FROM media_1)
I have read a few post on this, but not seeming to be able to fix my problem.
I am calling two database queries to populate two array's that run along side by side of each other, but they aren't matching, as the order that they come out is different. I believe i have something to do with the Group By, and this may require a sub query, but again a little lost...
Query 1:
SELECT count(bids_bid.total_bid), bidtime_bid, users_usr.company_usr, users_usr.id_usr
FROM bids_bid
INNER JOIN users_usr
ON bids_bid.user_bid = users_usr.id_usr
WHERE auction_bid = 36
GROUP BY user_bid
ORDER BY bidtime_bid ASC
Query 2:
SELECT auction_bid, user_bid, bidtime_bid, bids_bid.total_bid
FROM bids_bid
WHERE auction_bid = 36
ORDER BY bidtime_bid ASC
Even though the 'Order by' is the same the results aren't matching. The users are coming out in a different sequence.
I hope this makes sense, and thanks in advance.
* Update *
I just wanted to add a bit of clarity on what the output I want is. I need to only show 1 result by one user (user_bid) the second query show all users rows. I only need the first one to show the first row entered for each user. So if I could order before the the group and by min date, that would be ace...
It's to be expected. You're fetching fields that are NOT involved in the grouping, and are not part of an aggregate function. MySQL allows such things, but generally the results of the ungrouped/unaggregated functions can be wonky.
Because MySQL is free to chose WHICH of the potentially multiple 'free' rows to choose for the actual result row, you will get different results. Generally it picks the first-encountered 'free choice' result, but that's not defined/guaranteed.
You use grouping when you want unique results in result set according to some
group id (column name). usually grouping is used with aggregate functions such as
(min, max,count,sum..).
Ordering or inner query is nothing to do with result set, i suggest read some introductory
tutorials about grouping and think/treat Sql as a set based language and most of the set theory is applied on sql you'll be fine.
So I was complicating issues that I didn't need to. The solution I found was before.
SELECT users_usr.company_usr,
users_usr.id_usr,
bids_bid.bidtime_bid, min(bidtime_bid) as minbid FROM bids_bid INNER JOIN users_usr ON bids_bid.user_bid = users_usr.id_usr
WHERE auction_bid = 36
GROUP BY id_usr
ORDER BY minbid ASC
Thanks everyone for making me look (try) harder...
I have a table named countwronganswer with columns cwa_id, question_num. How can I generate a table with query that shows two columns, one column lists all the question_num and second column lists the number of times that cwa_id that related to the question_num.
Question Number |Total # of Mistake |
1 12
2 22
..etc
ATTENTION: This question was asked without the awareness of the existence of count or Groupby method because of the knowledge level at that state. Count() or Groupby() were the key to generate the 2nd column of total # values which I did not aware of completely, therefore, any attempt, at that point of time, to write the code for the data will be close to meaningless. Vote up if possible if you think its useful or resolved your issue.
Probably something like this
SELECT question_num, COUNT(cwa_id) total_mistakes
FROM countwronganswer
GROUP BY question_num
select question_num , count(cwa_id)
from tableName group by question_num
I'm trying to get the count of unique questions from the following mysql statement but every time I try to add count(q.id) as questionCount the statement only returns one result. I'm obviously doing something wrong but I can't figure out what it is.
http://www.sqlfiddle.com/#!2/34906/58
Hope somebody can help.
Steve
Just edit 2nd line of your query to this one:
select
count(distinct FinalQA.QUESTION_ID) from.....
It appears you want the total questions "stamped" on every row... for example you are auto-generating a test and want it to show "Out of 5 questions" in the output. To simplify this, since you KNOW you want 5 questions via your WHERE clause, I would slightly adjust it to...
select
FinalQA.*
from
( select
5 as TotalQuestionsOffered,
QWithAllAnswers.*,
... rest of query ) FinalQA
where
FinalQA.ARankSeq <= FinalQA.TotalQuestionsOffered
I have a fairly simple one-to-many type join in a MySQL query. In this case, I'd like to LIMIT my results by the left table.
For example, let's say I have an accounts table and a comments table, and I'd like to pull 100 rows from accounts and all the associated comments rows for each.
Thy only way I can think to do this is with a sub-select in in the FROM clause instead of simply selecting FROM accounts. Here is my current idea:
SELECT a.*, c.* FROM
(SELECT * FROM accounts LIMIT 100) a
LEFT JOIN `comments` c on c.account_id = a.id
ORDER BY a.id
However, whenever I need to do a sub-select of some sort, my intermediate level SQL knowledge feels like it's doing something wrong.
Is there a more efficient, or faster, way to do this, or is this pretty good?
By the way...
This might be the absolute simplest way to do this, which I'm okay with as an answer. I'm simply trying to figure out if there IS another way to do this that could potentially compete with the above statement in terms of speed.
Looks perfect to me.
Just wondering if it is ok with you to receive less or more than 100 rows from the above query.
Because if for a particular row of accounts table there are no rows in comments table then you would get less than 100 rows. Similarly if there are multiple matching rows in comments table for a row in accounts table then you may get more than 100 rows.
See: How can I optimize this query, takes more than a min to execute
No, The query is fine, just the way it is. You might want to filter the fields instead of a.* and c.* though.