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.
Related
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
I am trying to connect to the database in R using PostgreSQL. However, I have issues with quotation marks for database user - sql see user as some kind of systematic variable. Thus in the standard sql interface I would use
select count(*), email, name
from http_request_log hrl
inner join user_access_token uat on hrl.access_token_id = uat.id and uat.impersonated_by_id is null
inner join "user" u on uat.user_id = u.id
inner join customer c on u.customer_id = c.id
where hrl.created_time >= '2020-05-01'
group by u.email, c.name
order by count(*) desc
But in R, using dbGetQuery, there is an issue with quotation marks - I cannot use "user" and if I change to ' it does not work - it only works if the whole command starts and end with ' instead " - but then the where clause is not working as it does not recognize the date
uzivatele <- dbGetQuery(con,
"select count(*), email, name
from http_request_log hrl
inner join user_access_token uat on hrl.access_token_id = uat.id and uat.impersonated_by_id is null
inner join 'user' u on uat.user_id = u.id
inner join customer c on u.customer_id = c.id
where hrl.created_time >= '2020-05-01'
group by u.email, c.name
order by count(*) desc")
user is a reserved word in PostgreSQL.
One way to get around this is to prefix the table name with the schema name, e.g. public.user in your query.
Or you could try \"user\" in your query string.
I’ve got 3 tables:
Channel(id),
Content(id),
Post(id, channel_id, content_id).
Post holds information about content posted via specified channel.
How to get all the pairs content-channel which are not in Post table in MS Access.
You could obtain the result using a left join e.g.:
select q.ch, q.co
from
(select t.id as ch, u.id as co from channel t, content u) q
left join post p on q.ch = p.channel_id and q.co = p.content_id
where p.id is null
Here, the subquery uses a cross-join (aka cartesian product) to return all combinations of the Channel ID & Content ID, and the outer query then returns those combinations which aren't present in the Post table by virtue of the where clause.
You can use a cross join and then filter out the ones that exist:
select co.id, co.id
from channel as ch,
content as co
where not exists (select 1
from post as p
where p.channel_id = ch.id and p.content_id = co.id
);
Well, MS Access doesn't support CROSS JOIN syntax, so you have to use a comma for this.
Just out of curiosity:
I know that values should not be passed to a query due to concern of SQL injection.
I have a query similar to the one below. How can this be used in terms of SQL injection?
select * from users u inner join departments d on u.id = d.user_id
where u.id = '#{name}'
So the query that is made is
select * from users u inner join departments d on u.id = d.user_id
where u.id = 'An'
That query is just an example, not a working one.
Use the following for name:
' UNION SELECT username, password /* more fields */ FROM secrettable WHERE '' = '
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