SQL query: joining two tables with a where clause

I have a problem with SQL query. I received the following request, which is pretty simple:

SELECT * FROM table1 WHERE Date = CURDATE() 

Here is a column in table1 called h_id and a table2 with columns h_id and name

I want to join these two tables so that I have the names from table 2 from the current date.

I tried this but it does not work

 SELECT t2.name, t1.field2 FROM table1 t1 INNER JOIN table2 t2 ON t1.H_ID = t2.H_ID WHERE t1.Date = CURDATE( ) 
+4
source share
4 answers

This may be case sensitive.

or

Does table1 have a column field2?

If not / so, and as per your question, try like this:

 SELECT t2.name FROM table1 t1 INNER JOIN table2 t2 ON t1.h_id = t2.h_id WHERE t1.Date = CURDATE() 

Another possibility is where clause, try to do it like this:

 SELECT t2.name FROM table1 t1 INNER JOIN table2 t2 ON t1.h_id = t2.h_id WHERE convert(varchar, t1.Date, 112) = convert(varchar, getdate(), 112) 

The final possibility is that there is no h_id value from table1 and table2.

+7
source

try removing WHERE t1.Date = CURDATE( ) and see if your record is returned. If so, there is a problem with your CURDATE( ) , try using getdate () or try formatting the date

+2
source

SQL Server does not support curdate() . This is for MySQL.

The corresponding getdate() function

Here is the link to the official documentation

Edit:

As you said in the comment, if getdate() does not work, then you are using MySQL. Try the following:

 SELECT t2.name, t1.field2 FROM table1 t1 INNER JOIN table2 t2 ON t1.H_ID = t2.H_ID WHERE date(t1.Date) = date(CURDATE()) 
+2
source

Try:

 SELECT t2.name, t1.field2 FROM table1 t1 LEFT JOIN table2 t2 ON t1.H_ID = t2.H_ID WHERE t1.[Date] = GETDATE( ) 

or alternatively (MySQL):

 SELECT t2.name, t1.field2 FROM table1 t1 LEFT JOIN table2 t2 ON t1.H_ID = t2.H_ID WHERE t1.`Date` = CURDATE( ) 
0
source

Source: https://habr.com/ru/post/1399192/


All Articles