Query with the same fields in mysql - mysql

I'm trying to do this query.
I have these three tables
projects
id
name
authors
id
id_user
fk_id project
type = author, editor
in Table I have authors who is the creator and who is the editor
I'm trying to make a query in mysql where you can see in one record
who is the author and publisher.
I do the consultation with one of the two
SELECT p.id_project,p.name_project,a.fk_id_user,a.type,u.name
FROM projects p,users u, authors a
where p.id_project = a.fk_id_project
and a.type = 'author'
and a.fk_id_user = u.id_user
the result is
id_project, name_proyect, type, user, name_user
1 , bambo, author, 145, andrea
want is to do this
id_project, name_proyect, type, user, name_user, type2, user2, name_user2
1 ,bambo , author,145, andrea, editor, 785, barack
try to do this but not worked
SELECT p.id_project,p.name_project,a.fk_id_user,a.type,u.name,a.fk_id_user as user2, a.tipo as type2
FROM
projects p, users u, authors to
where p.id_project = a.fk_id_project
and a.type = 'author'
and a.fk_id_user = u.id_user
and user2 = u.id_user
and Typo2 = 'editor'
That type of query could use or if I'm doing wrong this.

suggest using LEFT or INNER JOIN authors a ON blablabla instead of having everything in the WHERE clause. However, try this:
SELECT
p.id_proyecto,
p.nombre_proyecto,
a.tipo,
a.fkidusario,
u.Nombres,
aa.tipo as type2
aa.fkidusario as user2,
uu.Nombres as name_user2
FROM proyectos p
JOIN usuarios u
JOIN usuarios uu
LEFT JOIN responsables a ON p.id_proyecto = a.fkidproyecto AND a.tipo = 'author' AND a.fkidusario = u.id_usuario
LEFT JOIN responsables aa ON p.id_proyecto = aa.fkidproyecto AND aa.tipo = 'editor' AND aa.fkidusario = uu.id_usuario

Related

How do i create two conditions in one mysql field?

Sorry I did not know how to word the question, it's actually a little more complicated than it sounds. I've got the following in my database; please see screenshot in the link:
http://prntscr.com/utyopv
I am generating a report using BIRT and I want that when priority = 2, it displays a value of 'High'. Similarly when priority = 1, it shows 'Medium' and when it = 1, it shows 'Low'.
But I also want that when estimated_time = 4, it displays '16+hrs', when estimated_time = 3, it shows '< 16 hrs' etc etc.
Now I've managed to do the priority using the code below;
SELECT date_created, CONCAT(first_name, " ", last_name) as user, e_name, cc_name, 'High' as priority, job_description, due_date, estimated_time, impediment
FROM job_planning as jp
INNER JOIN user as u
ON jp.user_id = u.id
INNER JOIN employer as e
ON jp.employer_id = e.id
INNER JOIN client_company as cc
ON jp.client_id = cc.id
WHERE jp.priority = '2'
union
SELECT date_created, CONCAT(first_name, " ", last_name) as user, e_name, cc_name, 'Medium' as priority, job_description, due_date, estimated_time, impediment
FROM job_planning as jp
INNER JOIN user as u
ON jp.user_id = u.id
INNER JOIN employer as e
ON jp.employer_id = e.id
INNER JOIN client_company as cc
ON jp.client_id = cc.id
WHERE jp.priority = '1'
union
SELECT date_created, CONCAT(first_name, " ", last_name) as user, e_name, cc_name, 'Low' as priority, job_description, due_date, estimated_time, impediment
FROM job_planning as jp
INNER JOIN user as u
ON jp.user_id = u.id
INNER JOIN employer as e
ON jp.employer_id = e.id
INNER JOIN client_company as cc
ON jp.client_id = cc.id
WHERE jp.priority = '0';
But how can I also include estimated_time? Can i use OR in mysql? I cannot use AND because then it would separate the two.
Thanks and appreciate any help.
SELECT ... ,
CASE priority WHEN 2 THEN 'High'
WHEN 1 THEN 'Medium',
ELSE 'Low'
END AS verbal_priority,
CASE estimated_time WHEN 4 THEN '16+hrs'
...

SQL get friends from friends table by username

Ok, I'm stuck with table design which I'm not able to change now and I need based on username to get all his friends from friends_relationship table
What I have now:
select distinct(username) from members m
inner join members_friends f on f.id_memb_friend1 = m.id
where (f.id_memb_friend1 = 173 or f.id_memb_friend2 = 173)
members table has columns: id, username
member_friends table has columns, id, id_memb_friend1, id_memb_friend2
The query I posted will return also username of member with id 173 who is the owner and his nickname for example is Splendid.
What I want to achive is to get all the friends together with username, based on username which I provide (Splendid = id 173).
Update:
I want to achive this:
Input: username=Splendid
Action: get usernames of splendid friends (together with their ids)
Output:
members.id = 5
username = John
members.id = 15
username = Lara
members.id = 66
username = Alex
and so on...
(Updated to accommodate the new requirement expressed in comments.)
(Updated to accommodate the new requirement expressed in the updated question.)
Your question is (was) inconsistent about whether you want to search based on username or user id. Although your query uses id, I use the username per the clarified requirement. I'm also supposing that you want the friends' usernames (only). Additionally, it appears from your initial query that you want to look at relationships in both directions (match on friend1, display all friend2, AND match on friend2, display all friend1).
Here's one way to do all that:
select mm.id, mm.username
from members mm
inner join member_friends f on f.id_memb_friend1 = mm.id
inner join members m on m.id = f.id_memb_friend2
where m.username = 'Splendid'
union distinct
select mm.id, mm.username
from members mm
inner join member_friends f on f.id_memb_friend2 = mm.id
inner join members m on m.id = f.id_memb_friend1
where m.username = 'Splendid'
SELECT CASE f.id_memb_friend1 WHEN '173' THEN f.id_memb_friend2 ELSE f.id_memb_friend1 END AS MemberID, m.username FROM members_friends f inner join members m on f.id_memb_friend1 = m.id OR f.id_memb_friend2 = m.id
where (f.id_memb_friend1 = 173 or f.id_memb_friend2 = 173)

MySql showed me no result with my query

I have 4 tables which are (model, license, pilot, and person)
Model: mid, name
License: mid, pid, licenseDate
Pilot: pid, hireDate
person: pid, firstName, lastName
I want to get the names of pilots who are not licensed to fly any airplane
I tried this query, but it showed me no result!!!
select model.mid, license.pid, license.mid, license.licenseDate, pilot.pid, pilot.hireDate, person.firstName, person.lastName
from model, license, pilot, person
where pilot.pid = license.pid and model.mid = license.mid and pilot.pid = person.pid
and not exists(SELECT null FROM model WHERE pilot.pid = license.pid);
try this... using inner join.
SELECT model.mid, license.pid, license.mid, license.licenseDate, pilot.pid, pilot.hireDate, person.firstName, person.lastName
FROM model
INNER JOIN license ON (model.mid = license.mid)
INNER JOIN pilot ON (pilot.pid = license.pid)
INNER JOIN person ON (pilot.pid = person.pid)
WHERE pilot.pid = license.pid AND model.mid = license.mid AND pilot.pid = person.pid
AND pilot.pid != license.pid);
Not tested the script, but You can fetch pilots which are not in License table means they're not licensed to fly a plane, then you can join the result with person table to fetch the particular name.
select p.firstname, p.lastname
from
(
select pid from pilot
where not exists (select pid from License where License.pid = pilot.pid)
) DT
inner join person p
on p.pid = DT.pid
Give this a try:
SELECT pe.firstName, pe.lastName FROM pilot p
LEFT JOIN license l ON p.pid = l.pid
JOIN person pe ON p.pid = pe.pid
WHERE l.pid IS NULL

mySQL reference correlation in a subquery with join

I'm working on a small social network service.
Very classically, I have 3 tables :
- table "circles_users" : all users that belong to the same "circle"
- table "friends" : friendship relation between users
- table "checkin" : is a user "checkin" somewhere or not
Here the structure of the database : http://sqlfiddle.com/#!2/27888/1
I would like to ask my database to give me :
all users from a specific "circle" with for each of them :
- the number of friends this user has in common with user id = 3
- if this user is checkin or not
Here what I'm trying to do :
SELECT a.uid,
(
SELECT COUNT(*)
FROM (SELECT IF (uid1 = 3, uid2, uid1) AS cf FROM friends WHERE (friends.uid1 = 3 OR friends.uid2 = 3 )) as b
JOIN friends ON ((friends.uid1 = b.cf AND friends.uid2 = a.uid) OR (friends.uid1 = a.uid AND friends.uid2 = b.cf))
) as common_friends,
checkin.status as checkin_status
FROM
(SELECT circles_users.uid FROM circles_users WHERE circles_users.circlename = 'circle_A') as a
LEFT JOIN checkin ON checkin.uid = a.uid
I get this error message : Unknown column 'a.uid' in 'on clause'
It has been now 2 days that I'm trying to fix this unsuccessfully.
It seems that it is not possible to reference correlation name in my subquery.
For instance, if I replace a.uid in the subquery by a specific uid (for instance let's say '4'), I don't get any error. But of course, the result is false...
Is there someone who could help me ?
That would be very nice :)
OTHER OPTION TO FOLLOW ?
Another option would be to pass the "common_friends" subquery as a join.
I tried to do something like this :
SELECT a.uid,
c.cnt as common_friends,
checkin.status as checkin_status
FROM
(SELECT circles_users.uid as uid FROM circles_users WHERE circles_users.circlename = 'circle_A') as a
LEFT JOIN
(
SELECT DISTINCT COUNT(*) as cnt
FROM (SELECT IF (uid1 = 3, uid2, uid1) AS cf FROM friends WHERE (friends.uid1 = 3 OR friends.uid2 = 3 )) as b
JOIN friends ON ((friends.uid1 = b.cf AND friends.uid2 = a.uid) OR (friends.uid1 = a.uid AND friends.uid2 = b.cf))
) as c ON 1=1
LEFT JOIN checkin ON checkin.uid = a.uid
But again : I get this error message : Unknown column 'a.uid' in 'on clause'Anyway, do you think this version would be easier to handle and would open new possibilities to resolve my problem ?
If you want to play with my queries : (thanks to #zundarz)
http://sqlfiddle.com/#!2/27888/1
Rewrote a query based on the info you give and what you are looking for.
SELECT cu.uid, COUNT(f.uid1) common_friends, c.status
FROM circles_users cu
LEFT JOIN friends f
ON (f.uid1 = cu.uid OR f.uid2 = cu.uid)
AND f.status = "on"
AND IF (f.uid1 = cu.uid, f.uid2, f.uid1) IN (
SELECT IF (uid1 = 3, uid2, uid1)
FROM friends
WHERE status = "on"
AND (friends.uid1 = 3 OR friends.uid2 = 3)
)
LEFT JOIN checkin c
ON c.uid = cu.uid AND c.status = "on"
WHERE cu.circlename = "circle_A"
GROUP BY cu.uid
Example sqlFiddle

How do I do a partial match on an IN statement in MySQL

I am joining multiple tables into a single query. I need to do a partial match on values in an IN statement. Here is an example.
SELECT DISTINCT
am.id AS id,
am.flagged AS flagged,
am.name AS name,
am.type AS type,
am.file AS file,
am.s3_tag AS s3_tag,
am.low_s3_tag AS low_s3_tag
FROM accounts_media am
LEFT JOIN accounts_location_media alm ON am.id = alm.media_id
LEFT JOIN accounts_location al ON al.id = alm.location_id
LEFT JOIN accounts_person_media apm ON am.id = apm.media_id
LEFT JOIN accounts_person ap ON ap.id = apm.person_id
LEFT JOIN accounts_event_media_record aemr ON am.id=aemr.media_id
LEFT JOIN accounts_medianote_media_record amma ON am.id=amma.media_id
LEFT JOIN accounts_medianote amn ON amma.medianote_id=amn.id
WHERE
am.account_id = '1234'
AND am.flagged = FALSE
AND ('Da' IN (SELECT first_name FROM accounts_person WHERE account_id = '1234')
AND ('Rob' IN (SELECT first_name FROM accounts_person WHERE account_id = '1234')
In the
AND ('Da' IN (SELECT first_name FROM accounts_person WHERE account_id = '1234')
statement there are values that say 'Dan', 'Daniel', etc. in the table. There is also 'Rob' and 'Robert'. I need that statement to make sure and name that contains 'Da' AND any name that contains 'Rob' from that table. Is there a way to do this?
So a record can be linked to multiple people in the accounts_person table. So lets say I have three records.
Record One: A person named Dan is attached to the record.
Record Two: A person named Robert is attached to the record.
Record Three: A person named Dan and a person named Robert are attached to the record.
I want the query to only return Record Three because it has the match of 'Da' and 'Rob'.
Try this:
WHERE
am.account_id = '1234'
AND am.flagged = FALSE
-- AND ( ap.first_name LIKE '%Da%' OR ap.first_name LIKE '%Rob%')
AND EXISTS
( SELECT 1
FROM accounts_person apx
WHERE apx.first_name LIKE '%Da%'
AND apx.account_id = am.account_id
)
AND EXISTS
( SELECT 1
FROM accounts_person apy
WHERE apy.first_name LIKE '%Rob%'
AND apy.account_id = am.account_id
)
I'm not sure, but I think you want a like statement, with a wild card after the Da.
SELECT DISTINCT
am.id AS id,
am.flagged AS flagged,
am.name AS name,
am.type AS type,
am.file AS file,
am.s3_tag AS s3_tag,
am.low_s3_tag AS low_s3_tag
FROM accounts_media am
LEFT JOIN accounts_location_media alm ON am.id = alm.media_id
LEFT JOIN accounts_location al ON al.id = alm.location_id
LEFT JOIN accounts_person_media apm ON am.id = apm.media_id
LEFT JOIN accounts_person ap ON ap.id = apm.person_id
LEFT JOIN accounts_event_media_record aemr ON am.id=aemr.media_id
LEFT JOIN accounts_medianote_media_record amma ON am.id=amma.media_id
LEFT JOIN accounts_medianote amn ON amma.medianote_id=amn.id
WHERE
am.account_id = '1234'
AND am.flagged = FALSE
AND (accounts_person.first_name like '%Da%'
OR accounts_person.first_name like '%Rob%')
AND accounts_person.account_id = '1234'