I have two table .
one meal table
id type
1 lunch
2 lunch
3 dinner
two user_history table
ID Meal_id User_id create
1 2 4 1404638939
Now I want to select all meal from table one but have condition
if table two meal_id match with Meal table id and that create date same as current date then skip that row from table one
I use this code but not work correctly
SELECT m.* FROM `meal` AS m LEFT JOIN user_history
ON user_history.meal_id != m.id and date(FROM_UNIXTIME(user_history.create))!=CURRENT_DATE() where m.meal_type = 'Lunch'
SELECT m.* FROM `meal` AS m
LEFT JOIN user_history
ON user_history.Meal_id != m.id and date(FROM_UNIXTIME(user_history.create))=CURRENT_DATE()
where m.type LIKE "%lunch%"
If I correctly understood the question, I'd say:
SELECT m.* FROM `meal` AS m
WHERE m.meal_type = 'Lunch'
AND m.id NOT IN (
SELECT meal_id FROM user_history
WHERE date(FROM_UNIXTIME(user_history.create))=CURRENT_DATE()
)
Related
Table Student
StudentID
StudentName
1
A
2
B
3
C
Table Book
BookID
BookName
1
Book1
2
Book2
3
Book3
Table BookAssignment
AssignID
BookID
StudentID
DateTime
1
1
1
2021-06-26
2
2
1
2021-07-01
3
1
2
2021-07-03
The result table should be
StudentID
StudentName
BookCount
1
A
2
2
B
1
3
C
0
How to get the result table in one SQL execution?
Left JOIN seems not an option since it eliminates StudentID 3
Just added another DateTime column to the BookAssignment table - What is SQL syntax to query the book count over the last 7 consecutive days (even for 0 book for day count)?
you need to use simple group by using left join between two tables:
select s.StudentID, s.StudentName , count(*) BookCount
from students s
left join books b
on s.StudentID = b.StudentID
group by s.StudentID, s.StudentName
I'd left join the student table on an aggregate query of the books and use coalesce to fill in the zeros:
SELECT s.StudentID, StudentName, COALESCE(cnt, 0)
FROM student s
LEFT JOIN (SELECT StudentID, COUNT(*) AS cnt
FROM books
GROUP BY StudentID) b ON s.StudentID = b.StudentID
You can also use a correlated subquery:
select s.*,
(select count(*)
from books b
where s.StudentID = b.StudentID
) as bookCount
from students s;
This has some advantages over using a join/group by approach:
You can trivially include all columns in the select. They don't have to repeated in the group by.
With an index on books(StudentID) this often has the best performance.
This avoids the outer aggregation, which can kill performance.
Adding another dimension (say the number of courses the student has) just works, without worrying about Cartesian Products.
select s.StudentID, s.StudentName ,(select count(*) from BookAssignment b where b.studentid = s.studentid) as BookCount
from students s
I have 3 tables.
person{personid,name, etc}
bid{bidid,personid,etc}
rating{ratingid,bidid,rating}
A person gets the rating after the bid is accepted by customers. So 1 bid = 1 rating.
And then the person bids another order, but the rating won't show up.
I already tried:
SELECT a.namausaha,ROUND(AVG(c.rating)) AS rating,a.kota,a.kontak,b.bidprice,a.mitraid
FROM tb_mitra a
JOIN tb_bid b ON b.mitraid=a.mitraid
LEFT JOIN tb_rating c ON c.bidid=b.bidid
WHERE b.orderid='OD004' AND b.statusbidid='1'
GROUP BY a.mitraid
but it doesn't work.
How to do it? I want to show the rating for every person.
Some rating return null due to left join
Try this
SELECT a.name,
ROUND(AVG(CASE WHEN c.rating IS NULL THEN 1 ELSE c.rating END )) AS rating,
a.etc,b.etc,a.personid
FROM person a
JOIN bid b ON b.personid=a.personid
LEFT JOIN rating c ON c.bidid=b.bidid
GROUP BY a.personid
SQL Fiddle
I have two tables:
cat_seriale - which represents the serial categories and provides ID's for each category like: 1, 2, 3, 4, 5.
seriale - which is actual tv serials, and each tv serial falls in one category.
I am trying to
SELECT * FROM cat_seriale WHERE `id`='1'
and additionally to all columns, display the sum of views column from all rows in seriale table.
If some one can help me out, that would be great.
Thanks in advance.
SCHEMA:
cat_seriale columns:
Primary Key - catid(int)
catname (varchar)
...
seriale columns:
Primary Key - id(int)
cat (int)
views(int)
I need to select cat_seriale where ID = 1, and select sum of views in seriale columns where cat is same with id from cat_seriale.
Like this:
select *, (select sum(views) from seriale S where S.cat=C.catid) as sum_views
from cat_seriale C
where id='1'
order by sum_views
Join the two tables.
SELECT c.*, SUM(s.views) AS views
FROM cat_seriale AS c
LEFT JOIN seriale AS s ON c.catid = s.cat
WHERE c.id = `1`
GROUP BY c.catid
+1 for #Mike answer, but this version has more performance:
SELECT c.*, SUM(s.views) as summary
FROM cat_seriale c
LEFT JOIN seriale s ON s.cat = c.catid
WHERE c.id = '1'
GROUP BY c.catid
ORDER BY summary
Following is my table for artists -
id name sex
1 harsh male
2 geet female
Following is my table for events -
id artist_id created_by
2 2 16
2 2 17
Following is my query -
SELECT * FROM `events` WHERE artist_id IN (SELECT id FROM `artists` WHERE name LIKE '%$search_term%')
But apart from events data I need to get artist name in the result as well, please let me know what I need to change in my query as I tried *, artists.name it wont worked.
Try this query
SELECT e.*
FROM `artists` AS a
JOIN `events` AS e ON e.artist_id = a.id
WHERE a.name LIKE '%$search_term%';
You need to select from two tables simultaneously. Use the join for that
SELECT artists.name, events.*
FROM artists
INNER JOIN events
ON artist.id = artist_id
WHERE
name LIKE '%search_term%'
Use a join instead of an IN
SELECT
e.*,
artists.name
FROM
`events` e
inner join `artists` a on e.artist_id = a.id
WHERE
name LIKE '%$search_term%'
Table 1 - Golfers
golferID
golferName
Table 2 - Picks
golferID
userID
I want to select all Golfers who have not been picked yet by a certain userID. The Picks table could have 20 entries for one golferID.
Assume you want all Golfers who have not been picked yet by userID 3, you can use
SELECT * FROM Golfers g
WHERE g.golferID NOT IN (
SELECT p.golferID FROM Picks p
WHERE p.userID = 3
)
You can use this:
select G.golferId, G.golferName
from Golfers G left join Picks P
on G.golferId = P.golferId
where P.userId != 'certain_user_id' or P.userId is NULL