How to Merge 2 SELECT statements

Asked

Viewed 132 times

-1

How to unite these 2 selects?

MY TABLES

   EVENT  TITLE DESCRIPTION CATEGORY_ID 
   CATEGORY NAME COLOR ICON DESCRIPTION

this select below works perfectly does the right Count I need to do the same without Count in the other two selects

  SELECT category.*,(SELECT COUNT(*) FROM event 
  WHERE category_id = category.id)as total FROM category

I’ve tried it this way

  "SELECT category.* FROM event WHERE category_id = 4) 
   FROM category ORDER BY MONTH(date_at)DESC "

In the other Table

    SELECT * FROM event WHERE DATE(date_at) = CURDATE()

   "SELECT category.* FROM event WHERE category_id = 5) 
   FROM category ORDER DATE(date_at) = CURDATE() "

I got it this way

SELECT  *, color
     FROM event , category 
    WHERE  event.id = event.category_id ; 

soh could not put the WHERE DATE(date_at) = KURDATE()

Someone ?

  • You will need to name the columns if they are not equal.

  • They have the same fields with the same types, could make available the layout and which fields will compose the two SQL?

  • What result do you expect for this query? The number of events for a specific category along with the category information?

  • @carloscoelho has to put the two tables in your question and indicate your comment also in the question?

  • No need to put it in capital letters, it seems to be screaming !

1 answer

1


Try this:

SELECT 

c.NAME,
c.COLOR,
c.ICON,
c.DESCRIPTION,
count(e.DESCRIPTION) as count_evt

FROM category c
join event e on e.category_id = c.id

group by 
c.NAME,
c.COLOR,
c.ICON,
c.DESCRIPTION;
  • I tested but does not return any echo I do not know why I need to make it work here it enters the category take the colors and take the title of the table It tried this here but did not give "SELECT Category. * FROM Event WHERE category_id = 4) FROM Category ORDER BY MONTH(date_at)DESC "

Browser other questions tagged

You are not signed in. Login or sign up in order to post.