Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql to select all from one table inner join with another table on some condition fails [closed]

Tags:

mysql

SELECT a.* FROM works AS a WHERE a.userid=15 INNER JOIN users AS b ON a.userid=b.id;

Something is just wrong and I don't know why, someone could offer some help please ? Thank you

like image 926
OhDude Avatar asked Mar 06 '12 02:03

OhDude


People also ask

How do you SELECT all records from one table that do not exist in another table?

How to Select All Records from One Table That Do Not Exist in Another Table in SQL? We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries.

Can we join 2 tables without on condition?

Yes, you can! The longer answer is yes, there are a few ways to combine two tables without a common column, including CROSS JOIN (Cartesian product) and UNION. The latter is technically not a join but can be handy for merging tables in SQL.

How do you join two tables based on conditions?

You join two tables by creating a relationship in the WHERE clause between at least one column from one table and at least one column from another. The join creates a temporary composite table where each pair of rows (one from each table) that satisfies the join condition is linked to form a single row.


1 Answers

Your WHERE clause is in the wrong position. It always follows all tables listed and joined in the FROM clause.

SELECT a.* 
FROM
   works AS a
   INNER JOIN users AS b ON a.userid=b.id
WHERE a.userid=15 ;
like image 163
Michael Berkowski Avatar answered Oct 05 '22 03:10

Michael Berkowski