I've the following tables:
permissions
user_permissions
users
When trying to run the following query:
SELECT * FROM users as u
LEFT JOIN user_permissions AS up ON u.id = up.id_user
LEFT JOIN permissions AS p ON up.id_permission = p.id
I'm getting the following error:
What I'm missing?
Here is the correct query:
SELECT * FROM users INNER JOIN (permissions INNER JOIN user_permissions ON permissions.id = user_permissions.id_permission) ON users.id = user_permissions.id_user WHERE (((users.username)='admin'));
According to #June 7 comment:
Access SQL is picky about parentheses for JOIN clauses (they are missing in your SQL). Use the query DesignView to get correct structure then switch to SQLView. The table aliases are not necessary
Related
I have this SQL
SELECT
U.id,
U.first_name,
hobby.id,
hobby.name
FROM
USER AS U
INNER JOIN user_hobbies AS UH
ON
UH.user_id = U.id
INNER JOIN hobby ON hobby.id = user_hobbies.hobby_id FOR JSON AUTO
am trying to run in in xampp PHP myadmin and am getting an error
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'JSON AUTO' at line 11
the FOR JSON AUTO is marked red even in SQL input could anyone help on how to so such inner join, return inner array
FOR JSON AUTO is SQL Server syntax, that is not supported in MySQL. If you are looking to generate one row per user, along with a JSON array of hobbies objects, you can use JSON aggregation like so:
select u.id, u.first_name,
json_arrayagg(json_object('id', h.id, 'name', h.name)) as hobbies
from users u
inner join user_hobbies uh on h.user_id = u.id
inner join hobbies h on h.id = uh.hobby_id
group by u.id
Note that this requires MariaDB 10.5.0 or higher.
SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id
Teacher_id being the session id,
I want to see all the students that have the same mentor.
Right now if I run this I just see all of the students twice, maybe one of you knows why?
My db scheme
You are not specifying on which columns you want to do the join, so you're getting a cross reference where all records are joined to all records.
You should do something like (not sure about your column names):
SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id
I have two table country and Users. I want to view the country name which have user disabled. So i wrote query for this.
SELECT DISTINCT cntr_id,cntr_name FROM
(SELECT COUNTRY.cntr_id, COUNTRY.cntr_name, USERS.user_enabled,
USERS.user_name, USERS.user_id
FROM COUNTRY INNER JOIN Users
ON COUNTRY.cntr_id = USERS.cntr_id
)
AS TAB where user_enabled = 0
My questions are :
Is this inner query?
will the query fetch all the countries (include user enabled) from the database before running outer query?
Is there any other method to select?
Yes it's an inner Query and will fetch what you need, but you don't need your outer SELECT query. You could just do it like:
SELECT DISTINCT COUNTRY.cntr_id, COUNTRY.cntr_name
FROM COUNTRY INNER JOIN Users
ON COUNTRY.cntr_id = USERS.cntr_id
WHERE USERS.user_enabled = 0
Yes, that is inner query.
Yes, inner query will fetch all the countries (including user enabled). Outer query will remove those records later.
You don't have to use outer query for this.
SELECT DISTINCT C.cntr_id, C.cntr_name
FROM COUNTRY C INNER JOIN
Users U ON C.cntr_id = U.cntr_id
WHERE U.user_enabled=0
OR
SELECT C.cntr_id, C.cntr_name
FROM COUNTRY C INNER JOIN
Users U ON C.cntr_id = U.cntr_id
WHERE U.user_enabled=0
GROUP BY C.cntr_id, C.cntr_name
To answer your question,
Yes It is inner and outer two queries.
The inner query will fetch the all rows which satisfy the join condition.
Yes there is other method to select where you can directly apply the filter in inner query and remove the outer select.
SELECT COUNTRY.cntr_id, COUNTRY.cntr_name, USERS.user_enabled,
USERS.user_name, USERS.user_id
FROM COUNTRY INNER JOIN Users
ON COUNTRY.cntr_id = USERS.cntr_id
WHERE
USERS.user_enabled = 0
I'm trying to run this command from the shell:
sqlite3 salesShare1.db "
select
UsersSale.saleSpecificProduct,
UsersSale.fAtMall,
Users.name,
Users.gender,
Stores.storeName
FROM
UsersSale
INNER JOIN (Users INNER JOIN Stores ON Users.userID = UsersSale.userID) ON
Stores.storeID = UsersSale.saleStoreID
ORDER BY UsersSale.saleID";
Error: no such column: Users.name
I created all the tables and columns.
sqlite3 salesShare1.db "select * FROM Users ";
1|hezi|0|||
'hezi' is Users.name
What am I doing wrong ?
try please
select us.saleSpecificProduct,
us.fAtMall,u.name,
u.gender,st.storeName
from UsersSale us,Users u,Stores st
where u.userID=us.userID and st.storeID=us.saleStoreID
order by us.saleID
or
select us.saleSpecificProduct,
us.fAtMall,u.name,
u.gender,st.storeName
from UsersSale us
inner join Users u
on u.userID=us.userID
inner join Stores st
on st.storeID=us.saleStoreID
order by us.saleID
hope it helps
Display the schema for your table in the sqlite3 tool and confirm your column name is what you thought it was. Maybe you mis-typed the name.
Your inner join is a little ackward.
Shot in the dark, but my guess is you need to join in the Users table:
select
us.saleSpecificProduct,
us.fAtMall,
u.name,
u.gender,
s.storeName
FROM
UsersSale us
INNER JOIN Users u ON u.userID = us.userID
INNER JOIN Stores s ON s.storeID = us.saleStoreID
ORDER BY us.saleID
I'm trying to do a pretty complex query in MySQL; complex for me, at least.
Here's an example of what I'm trying to do:
SELECT * FROM friends
LEFT JOIN users ON users.uid = friends.fid1
LEFT JOIN users ON users.uid = friends.fid2
WHERE (friends.fid1 = 1) AND (friends.fid2 > 1)
UNION SELECT fid2 FROM friends
WHERE (friends.fid2 = 1) AND (friends.fid1 < 1)
ORDER BY RAND()
LIMIT 6;
I'm getting back: ERROR 1066 (42000): Not unique table/alias: 'users'.
Where am I going wrong, and how should I really be performing this query?
Alias your table, like:
LEFT JOIN users u1 ON u1.uid = friends.fid1
LEFT JOIN users u2 ON u2.uid = friends.fid2
You have written left join two times with different fields of tables, it seems to When it got parse so give them alias and then join them with friends Table
LEFT JOIN users users1 ON users1.uid = friends.fid1
LEFT JOIN users users2 ON users2.uid = friends.fid2