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.
Related
I have a column named level in my mysql database.
I am trying to write an sql statment and one of the parameters is level of course. But I get an error. I know it's because I am using a protected word so how can I still use it in my statement. Do I put it inside commas or something?
From the comments
1052 - Column 'level' in where clause is ambiguous
The above error message is telling that more than one table has column named level and so you have to prefix level column with table alias of the table whose column level you want to compare in WHERE clause. In below query I am assuming you want schools table level column so use it like this s.level
SELECT u.ck_id,u.firstname, u.lastname, sc.class_description,
s.name, GROUP_CONCAT(correct)
FROM game_statistics gs
INNER JOIN users1 u ON u.id = gs.user_id
INNER JOIN school_classes sc ON sc.id = u.class_id
INNER JOIN schools s ON s.id = sc.school_id
WHERE game_type = 'exammultiple' AND gs.created_at >= '2018-03-01'
AND time >='40' AND s.`level` = '6'
GROUP BY user_id
ORDER By name ASC, class_description ASC
You can change to correct table alias as intended by you.
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.
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
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
I have a user table from which I want all values, so I have this query:
SELECT tbl_user.* FROM tbl_user
Now I want one additional column in this result which shows all roles this user has, (or nothing if there are no roles for the user). The role information comes from two additional tables.
The first table contains these two values: userid, roleid
The second table contains roleid and role_name.
So the group concat needs to get all role names based on the roleid's in table1.
I have tried several different ways to do this, but I don't succeed. Either I get only one result with several times the same rolename, or no result at all.
Thanks for your help
Michael
Update: added LEFT JOIN for users with no role.
SELECT
tbl_user.*,
GROUP_CONCAT(role_name) AS roles
FROM
tbl_user LEFT JOIN tbl_roles ON tbl_user.userid = tbl_roles.userid
JOIN tbl_rolenames ON tbl_roles.roleid = tbl_rolenames.roleid
GROUP BY tbl_user.userid
Note that MySQL will permit a GROUP BY on fewer columns than appear in the SELECT list in total, but in other RDBMS you would need to explicitly list out the columns in tbl_user and include them in the GROUP BY, or do an additional self join against tbl_user to get the remaining columns from that table.
Something like:
SELECT
urole.userid,
uall.username,
uall.name,
uall.othercols,
urole.roles
FROM
tbl_user uall JOIN (
SELECT
tbl_user.userid,
GROUP_CONCAT(role_name) AS roles
FROM
tbl_user LEFT JOIN tbl_roles ON tbl_user.userid = tbl_roles.roleid
JOIN tbl_rolenames ON tbl_roles.roleid = tbl_rolenames.roleid
GROUP BY tbl_user.userid
) urole ON uall.userid = urole.userid