I can imagine that question exists in some way, but I haven't found what I was looking for.
I need to remove the found values from the result I'm getting in the main query.
Consider the following:
The mainquery:
SELECT idTable
FROM tblTables
WHERE NOT IN idTables = ( **SUBQUERY HERE** )
AND dtSeats >= 4
LIMIT 1;
The subquery:
SELECT idTable
FROM tblTables,tblReservation
WHERE tblTables.idTable = tblReservation.fiTable
AND fiTime = 1
AND dtResDate = "2020-06-16"
In the tblTables there are idTable and dtSeats.
In the tblReservation are fiTime and dtResDate.
The subquery can get up to three rows.
I need to get the first free table with the lowest number of seats possible.
Thanks for helping me out!
Having the DDL and some sample data would be helpful, but I think what you are looking for is a NOT EXISTS clause. It returns everything in the outer query that doesn't match with a record in the inner query.
SELECT idTable
FROM tblTables tt
WHERE NOT EXISTS (
SELECT NULL FROM tblReservation tr WHERE tt.idTable = tr.idTable AND
tr.dtResDate = '2020-06-16'
)
AND dtSeats >= 4
ORDER BY tt.dtSeats
LIMIT 1
Related
I want make a leaderboard from my database, I already make a code but the sql says error
"SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun)
MySQL said: Documentation
#1242 - Subquery returns more than 1 row"
heres my sql query :
SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun);
SET #a2 = (SELECT SUM(j.nilai_jawaban)
FROM jawaban j
GROUP BY j.id_akun);
SET #hasil = #a1 + #a2;
SELECT #hasil
Thank you !
this is an example data in table jawaban:
this is an example data in table diskusi:
and my desired answer:
where tera123 have 135 TOTAL(d.poin_diskusi+j.nilai_jawaban) and david123 have 90 TOTAL(d.poin_diskusi+j.nilai_jawaban)
the point is to show TOTAL for every user
It looks to me (without looking at your images; if you want to show information, please provide it in text) like you want something like:
select id_akun, sum(point_value) as total_points from (
select id_akun, sum(poin_diskusi) as point_value
from diskusi group by id_akun
union all
select id_akun, sum(nilai_jawaban) as point_value
from jawaban group by id_akun
) as point_values
group by id_akun
The problem are the GROUP BY keywords. Inside the images you provided with example data, one can see that there are multiple entries for the columns jawaban.id_akun and diskusi.id_akun. Because of that, both SELECT will return multiple rows as result, for each different value of the id columns one summed up row. Try it without the GROUP BY clause.
I searched a lot and tried many queries but not getting satisfied answer. So like to ask.
I am looking for last 5 records from mysql table if having same value otherwise not.
Something like if col_n is having same value x from last 5 records then count otherwise not. But I am not able to figure out how to write query for this ?
SELECT count(col_n)
from track if(last five col_n = 'ok')
WHERE col_a = 'value1' AND col_b = 'value2'
enter mysql table records
Please try this:
SELECT p.*
FROM
( SELECT *
FROM demo ORDER by id DESC
LIMIT 5
) AS p
JOIN
( SELECT COUNT(*) AS cnt
FROM
( SELECT 1
FROM demo
LIMIT 5
) AS tmp
) AS c
ON c.cnt = 5 WHERE p.name='x'
I want to count the rows of SQL statement with GROUP BY. Here is my sql statement:
I want to get the count of these rows
SELECT RETAILER_ID, COUNT(RETAILER_ID) AS TOTAL
FROM ret_retailer
LEFT JOIN temp_sg_screen_sto_72_mos_summary ON ret_retailer.RETAILER_FULL_NAME = temp_sg_screen_sto_72_mos_summary.RETAILER_NAME
WHERE MAY_2018 < 100000
GROUP BY RETAILER_ID
Here:
select count(*)
from (
-- insert your query here
) sub
You can simply use count(distinct):
SELECT COUNT(DISTINCT r.RETAILER_ID)
FROM ret_retailer r LEFT JOIN
temp_sg_screen_sto_72_mos_summary s
ON r.RETAILER_FULL_NAME = s.RETAILER_NAME
WHERE MAY_2018 < 100000 ;
The only difference from your results if if RETAILER_ID could ever be NULL. It is easy enough to adjust for NULL values, but I'm guessing that it is never NULL.
So, I'm getting data from one table with a simple SELECT statement with SELECT COUNT included.
SELECT *,
(SELECT COUNT(`message_id`)
FROM `user_message`
WHERE `status_to` = 0
) AS `unread`
FROM `user_message`
Here, unread counts for unread messages coming from other users. But that's not quite enough. What I want is to reference SELECT COUNT to a specific column, user_id, within the same table:
SELECT *,
(SELECT COUNT(`message_id`)
FROM `user_message`
WHERE `status_to` = 0 AND `user_message`.`user_id` = `user_message`.`user_id`
) AS `unread`
FROM `user_message`
.. if that makes sense. My second statement disregards this part: AND user_message.user_id = user_message.user_id, and gives me the same result for each user_id.
What am I missing??
You need to give different aliases to your table to get the related count
SELECT *, (
SELECT COUNT(`message_id`)
FROM `user_message` b
WHERE `status_to` = 0
AND `a`.`user_id` = `b`.`user_id`
) AS `unread`
FROM `user_message` a
I have a MySQL statement that I need a little bit of help with. I am pulling the data correctly but I just need a some help excluding the records that are not need. If the candidate remaining attempts equal 0 I do not want to pull that record, but as you can see the I am using a subquery to generate the number of remining attempts a candidate may have.
Is there a way to use the remaining_attempts field in the WHERE statement to exclude the un-wanted records?
SELECT (
SELECT count(*) AS Number_Attempts_Used
FROM candidate_exam_record
WHERE candidate_exam_record.idcandidate_eligibilty = candidate_eligibilty.idcandidate_eligibilty
) AS remaining_attempts
, (
SELECT CASE WHEN count(*) > '0' THEN candidate_exam_record.idcandidate_eligibilty END
FROM candidate_exam_record
WHERE candidate_exam_record.idcandidate_eligibilty = candidate_eligibilty.idcandidate_eligibilty) AS EL_ID
FROM candidate_eligibilty
INNER JOIN candidate ON candidate_eligibilty.idcandidate = candidate.idcandidate
INNER JOIN exam ON candidate_eligibilty.exam_id = exam.exam_id
INNER JOIN jurisdiction ON exam.jurisdiction_id = jurisdiction.jurisdiction_id
WHERE jurisdiction.jurisdiction_id = 'xxxx'
AND candidate_eligibilty.eligibility_end_date >= '2013-02-19'
AND remaining_attempts > '0'
It's messy but it looks OK to me. Try changing '0' to just plain old 0. You're storing it as an INT I assume, correct?