SQL JOIN cant get it working - mysql

I know the forum is full of this questions but i cant find the solution.
I want to join the table users from user_to_designment they both have a column with user_id the error that i get is:
select user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id
LIMIT 0, 25
MySQL meldt: Documentatie
#1052 - Column 'user_id' in field list is ambiguous
I use this query:
select user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id
Please some advice

In your select list prefix the user_id with the table name:
select users.user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id
Both columns have user_id, SQL cannot choose between them, you must specify explicitly.

You need to specify which user_id to return in your select, eg select users.user_id or Select user_to_designment.user_id

You must clarify which user_id table column you are selecting.
select a.user_id,b.designment_id from user_to_designment b
full join users a
on b.user_id=a.user_id

This is because "user_id" field exists on both tables.
you must put table name before field name.
select user_to_designment.user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id

It seems user_id exist in both the tables. so it gives the ambiguous error. We should say from which table we have to pick the column.
SELECT users.user_id
,user_to_designment.designment_id
FROM user_to_designment
FULL JOIN users ON user_to_designment.user_id = users.user_id
You may use either users.user_id or user_to_designment.user_id in your select statement. Always use tablename.columnname format. It avoids confusion.

Related

Query Check Status from other table

I have following query
select * from user_profile
Now i want to add where condition check user status from other table (users)
select * from user_profile,users where users.status!=0
Please Do Not Recommend Join i following old join query
Thanks
If you don't want to use join, you have to use a subquery. I guess both tables have a column like userId:
select * from user_profile where userId in (select userId from users where status != 0)
try this using sub query
select * from user_profile where status!=(select status from users where id =? or status=?)
what you compare id or status in subquery
Assuming you have a relation column (say user_id or perhaps profile_id or something -- not sure until you share sample data as requested.) between the two tables, you can join the two table and filter the rows like this:
select *
from user_profile p
join users u on p.user_id = u.user_id
where u.status != 0;
If you want every column from both tables in your query, you can use:
SELECT * from user_profile, users WHERE user_profile.user_id = users.id AND users.status! = 0
Where I assumed you have some column(like user_id - user_profile
and id - users in my example) that links both tables together.

#1052 - Column 'status' in field list is ambiguous

SELECT
repeats.id,user_id,deposit_id,repeat_time,made_time,rebeat,status,created_at,updated_at
FROM repeats
INNER JOIN users ON users.id = repeats.user_id
I am trying to merge two tables; users and repeats, but it is giving following error.
Error
SQL query: Documentation
SELECT repeats.id
,user_id
,deposit_id
,repeat_time
,made_time
,rebeat
,status
,created_at
,updated_at
FROM repeats
INNER JOIN users
ON users.id = repeats.id
LIMIT 0, 25
MySQL said: Documentation
#1052 - Column 'status' in field list is ambiguous
Both your repeats and users tables seem to have a status column. You need to fully qualify the column in your query. E.g.:
SELECT repeats.id,
user_id,
deposit_id,
repeat_time,
made_time,
rebeat,
repeats.status, -- Or users.status, depending on what you need
created_at,
updated_at
FROM repeats
INNER JOIN users ON users.id = repeats.user_id
Whenever you have more than one table in a query, you should also qualify the column names. Another good practice is to use table aliases that are abbreviations for the table names.
I don't know what your data looks like, but the query would be something like:
SELECT r.id, r.user_id, r.deposit_id, r.repeat_time, r.made_time, r.rebeat,
u.status, u.created_at, u.updated_at
FROM repeats r INNER JOIN
users u
ON u.id = r.user_id;
This is just a guess. You have to correctly qualify the names.

Select rows that are referenced in another table

I have two tables and they are as follows:
USERS
ORDERS
I want select all users who have at least 1 order or more in the ORDERS table. I know there is an inline query for this in MySQL, but right now I have to select all users and then make another query seeing if each user has an order - all this using a PHP loop.
What I am doing now is not ethically correct, so I basically just want to select all users who have been referenced in the ORDERS table in ONE MySQL query.
This is a query you should be using
select distinct u.* from users u
inner join orders o on o.user_id = u.id;
Note the distinct and u.*. This query will not select fields from orders and it will not select the same user twice (if one has more than one order).
Demo: http://sqlfiddle.com/#!2/6ebcc/3
You can use mysql join syntax. Assuming both of your tables has userid column, this is the example :
SELECT * FROM USERS a JOIN ORDERS b ON
a.UserId = b.UserId
This is a simple database operation, see here for the explanation join

MySQL select rows that do not have matching column in other table

I can't seem to figure this out so far. I am trying to join two tables and only select the rows in table A that do not have a matching column in table B. For example, lets assume we have a users table and a sent table.
users table has the following columns: id, username
sent table has the following columns: id, username
I want to select all rows from users where username does not exist in sent table. So, if tom is in users and in sent he will not be selected. If he is in users but not in sent he will be selected. I tried this but it didn't work at all:
SELECT pooltest.name,senttest.sentname
FROM pooltest,senttest
WHERE pooltest.name != senttest.sentname
Typically, you would use NOT EXISTS for this type of query
SELECT p.Name
FROM pooltest p
WHERE NOT EXISTS (SELECT s.Name
FROM senttest s
WHERE s.Name = p.Name)
An alternative would be to use a LEFT OUTER JOIN and check for NULL
SELECT p.Name
FROM pooltest p
LEFT OUTER JOIN senttest s ON s.Name = p.Name
WHERE s.Name IS NULL
Note that the implicit join syntax you are using is considered obsolete and should be replaced with an explicit join.
Try this SQL:
SELECT users.username
FROM users
LEFT JOIN sent ON sent.username = users.username
WHERE sent.username IS NULL;
The better way in my opinion would be:
SELECT users.username
FROM users
LEFT JOIN sent ON sent.id = users.id
WHERE sent.id IS NULL;
As both the id fields, would be indexed (primary key I would have thought) so this query would be better optimised than the first one I suggested.
However you may find my first suggestion better for you, it depends on what your requirements are for your application.
May be this one can help you ....
I had also the same problem but Solved using this this query
INSERT INTO tbl1 (id,name) SELECT id,name from tbl2 where (name) not in(select name from tbl1);
hope this one will solve your problem

Joining multiple columns from table a to one on table b

I've spent a bit of time researching this on here and the mysql site but I'm a bit confused on two things: which sort of join to use and how (or if) to use an alias.
The query:
SELECT forum_threads.id, forum_threads.forum_id, forum_threads.sticky,
forum_threads.vis_rank, forum_threads.locked, forum_threads.lock_rank,
forum_threads.author_id, forum_threads.thread_title, forum_threads.post_time,
forum_threads.views, forum_threads.replies, users.username AS author_username
FROM forum_threads LEFT JOIN users ON forum_threads.author_id = users.id
WHERE forum_threads.forum_id=XXX
Now that query currently finds all threads from the given forum and joins the threads author id to the username table. I also have lastpostid which I'd also like to include in that query and join again on the users table so I can get the username for the last poster too.
I tried adding:
LEFT JOIN users ON threads.lastpostid = users.username
but that just results in an alias error as users isn't unique.
I also tried using both an alias on the main query and on the second join but it keeps giving me missing field errors.
Could someone give me a point in right direction please?
Yes, you need a different alias each time. Every time you refer to the table in the query you should use the approprate alias.
SELECT
forum_threads.id,
-- etc...,
forum_threads.replies,
u1.username AS author_username
u2.username AS last_post_username
FROM forum_threads
LEFT JOIN users u1 ON forum_threads.author_id = u1.id
LEFT JOIN users u2 ON threads.lastpostid = u2.username
WHERE forum_threads.forum_id=XXX