Query to get data form different columns - mysql

I got a problem regarding my query, I cannot seem to get the right one to get the data I want. I've got the following tables I need to get data from:
app_exhibition
- id
- name
- form_id
app_forms
- id
- name
app_languages
- id
- name
- code
app_link_lang
- id
- form_id
- lang_id
I want to select the name of the exhibition (app_exhibition) the id and name of the form (app_forms) the name of the language (app_languages), and I know which one to select (language wise) because it's linked in the app_link_lang table.
Now I got the following query, but that is (obviously) not working..:
select f.form_id, f.name, b.name as beursnaam, l.name as languagename
FROM app_forms f, app_lang_link ll
WHERE f.id=1
LEFT JOIN app_beurzen b ON (b.form_id = f.id)
LEFT JOIN app_languages l ON ll.form_id = f.id

This should do the job:
SELECT e.name ename, f.id fid, f.name fname, l.name lname
FROM app_exhibition e
INNER JOIN app_forms f ON f.id=e.form_id
INNER JOIN app_link_lang al ON al.form_id=f.id
INNER JOIN app_languages l ON l.id=al.lang_id
You forgot to weave in the link table app_link_lang providing the m-to-n link. The result can now consist of several lines per exihibition, depending on the languages linked to it in app_link_lang.
Edit
I extended your fiddle (filled in some data) and it works, see here: http://sqlfiddle.com/#!9/a86be/1

Related

SQL INNER JOIN / Unknown column C.book in On Clause

https://www.db-fiddle.com/f/nCgygDjwYXZnWQ28LL7kMi/0
Goal:
Select copy.owner of people that own both books by "Scott Kelby" with different ISBN numbers
Hey guys, in order to derive the book table in the example on db-fiddle, I had actually used a bunch of inner join statements.
Now that I have acquired the book table how can I check that the c.owner owns both copies of the book?
I tried to use
SELECT DISTINCT
C.owner
FROM
copy C,
copy C1
INNER JOIN (
SELECT
B.authors,
B.ISBN13
FROM
book B
INNER JOIN(
SELECT
B1.authors
FROM
book B1
GROUP BY
B1.authors
HAVING (COUNT(B1.authors) > 1)
) B1
ON B.authors = B1.authors) B
ON C.book = B.ISBN13 AND C1.book = B.ISBN13 AND C1.book <> C.book;
However, if I try to reference to two table in the 2nd line of code, i will get the error unknown column in ON clause forcing me to remove copy C1 from the query.
You can try below -
select owner
from book b inner join copy c
on b.isbn13=c.book
group by owner
having count(distinct isbn13)=2

MySQL Join Query 4 tables

I'm trying to create a query with multiple joins, but it's not working for me.
I have the follow query statement:
SELECT DISTINCT s.per_id
, s.per_key
, s.per_disabled
, p.acc_id
, a.acc_name
, p.approved_acc_id
, p.per_time
FROM acc_permissions p
JOIN svr_permissions s
JOIN acc_general a
JOIN svr_group_permissions g
WHERE a.acc_id = p.acc_id
AND p.per_id = s.per_id
OR a.acc_per_group = g.group_id
AND a.acc_id = p.acc_id
Now if you don't want to examine this query, I understand so I will example my table structure;
First Table (includes users):
acc_general
Second table (includes permissions (linked to users)):
acc_permission
- This table includes rows that are linked to the acc_id in acc_general.
Multiple rows are possible for one unique acc_id in this table.
Third table (includes permissions (liked to groups)):
group_permissions
Now this includes rows that are linked to groups, each group has multiple rows in this table.
Inside acc_general there is a field called; acc_group_id, this is liked with the group_id inside group_permissions
So I need a query that returns all permissions from all players.
But it should not create duplicated permissions for a account.
So if I have an account that has a permission id 1 inside acc_permission and it has permission id 1 inside group_permissions it should ignore it.
It's hard to example, but i hope someone understands what I want.
Regards, Roel
join syntax for your query
SELECT DISTINCT s.per_id AS per_id,
s.per_key AS per_key,
s.per_disabled AS per_disabled,
p.acc_id AS acc_id,
a.acc_name as acc_name,
p.approved_acc_id AS approved_acc_id,
p.per_time AS per_time
from acc_permissions p
join svr_permissions s
ON p.per_id = s.per_id
join acc_general a
ON a.acc_id= p.acc_id
left join svr_group_permissions g
ON a.acc_per_group = g.group_id

MYSQL query not working as wanted

I want to be able to get the pieces evidence within a trybe. But I have to go through the ambition table which is the relation from trybe to evidence.
So basically get count(e.id) from
The query i have at the moment is as follows:
$evidence = array(1,2,10,20,40,60,80,100,200,300,400,500,1000,1500,2000);
$evidence = implode(", ", $evidence);
# no of evidence in trybe = the x trybe now has over x evidence
$query = sprintf("SELECT t.id, t.name, COUNT(e.id) as number, 'trybe_evidence' AS type
FROM x_trybes t
INNER JOIN x_ambition_owner ao ON (t.id = ao.trybe_id)
LEFT JOIN x_evidence e ON (ao.id = e.ambition_id)
GROUP BY t.id, t.name
HAVING COUNT(e.id) in (%s)",$evidence);
How can I get all the pieces of evidence from all the ambitions within the trybe table?
The structure of the tables are as follows:
x_trybes
id name
x_ambition_owner
id ambition_id trybe_id
x_ambition - ambition info
id name etc.
x_evidence
id ambition_id name
Please note there can be many trybes, many ambitions and many pieces of evidence.
Thanks

How to get multi columns via subquery?

I want to get members and their photos. Every member has 2 photos. (I am not talking about profile image)
There are 2 tables named as Members and MemberPhotos.
Here is my query which doesn't work(expectedly):
SELECT
M.Name as MemberName,
M.LastName as MemberLastName,
(
SELECT
TOP 1
MP.PhotoName
FROM
MemberPhotos MP
WHERE
MP.MemberID = M.ID
AND
MP.IsFirst = 1
) as MemberFirstPhoto,
(
SELECT
TOP 1
MP.PhotoName
FROM
MemberPhotos MP
WHERE
MP.MemberID = M.ID
AND
MP.IsFirst = 0
) as MemberSecondPhoto,
FROM
Members M
Maybe somebody going to say that I should use inner join instead, I don't want to use inner join, if I use it I get data multiple like:
Name Surname PhotoName
Bill Gates bill.png
Bill Gates bill2.png
Steve Jobs steve.jpg
Steve Jobs steve2.jpg
What do you recommend me about query?
Thanks.
EDIT:
Here is the output I want to get:
Name Surname FirstPhoto SecondPhoto
Bill Gates bill.png bill2.png
Steve Jobs steve.jpg steve2.png
The only issue with your example query is that you have an extra comma after
as MemberSecondPhoto
If you remove this it works fine.
SQL Fiddle with demo.
However, while that query is working now, because you know that each member only has two photos, you can use a much simpler query:
SELECT
M.Name as MemberName,
M.LastName as MemberLastName,
MPF.PhotoName as MemberFirstPhoto,
MPS.PhotoName as MemberSecondPhoto
FROM Members M
LEFT JOIN MemberPhotos MPF ON M.ID = MPF.MemberID AND MPF.IsFirst = 1
LEFT JOIN MemberPhotos MPS ON M.ID = MPS.MemberID AND MPS.IsFirst = 0
SQL Fiddle with demo.

MySQL - Using column value for joining in the same query

I have three tables that looks something like this:
Table joins
|ID|JOIN_NAME|
1 persons
2 companies
Table information
|ID|JOIN_ID|
1 1
2 2
Table information_extra_persons
|ID|INFORMATION_ID|NAME|
1 1 John
Table information_extra_companies
|ID|INFORMATION_ID|NAME|
1 2 IBM
How can i join together these tables in one SQL? I've tried something like:
SELECT * FROM `information`
INNER JOIN `information_extra_(SELECT `name` FROM `joins` WHERE `id` = `join_id`)`
ON `information_extra_(SELECT `name` FROM `joins` WHERE `id` = `join_id`)`.`information_id` = `information`.`id`
but I can't get it to work. Of course this isn't my actual table setup, but it's the same principle. Does anyone know how to get all the info in just one SQL?
That's actually four tables, not three. This isn't just a nitpick - it looks as though the substance of your question is "how can I use the name of the table as part of the join criteria?" (ie. how can the information_extra_ tables be treated as a single table?)
To which the answer is: you can't. (Outside of dynamic SQL.)
In this specific case, the following should return what I think you are looking for:
select j.join_name joined_entity,
case when j.join_name = 'persons' then p.name
else c.name
end joined_entity_name
from information i
inner join joins j on i.join_id = j.id
left join information_extra_persons p on i.id = p.information_id
left join information_extra_companies c on i.id = c.information_id
Alternatively, a less efficient (but more general) approach might be:
select j.join_name joined_entity,
v.name joined_entity_name
from information i
inner join joins j on i.join_id = j.id
inner join (select 'persons' entity, information_id, name from information_extra_persons
union all
select 'companies' entity, information_id, name from information_extra_companies) v
on i.id = v.information_id and j.join_name = v.entity