SQL get information from 4 tables - php

I have a database from which I need to JOIN 4 tables and I am having some trouble figuring it out
I have the following tables
A. id, name, picture, d_id
B. id, name
C. id, a_id, b_id, commentaar
D. id, name
Does anyone have any idea how to go about this? A has data used in D and C has data used in A and B, so I am at a loss how to get this one done. I want to output
A.name, A.picture, B.Name, C.commentaar, D.name

Unless I'm missing something, you just need to do this:
Select A.name, A.picture, B.Name, C.commentaar, D.name
From A
Join D On A.d_id = D.id
Join C On C.a_id = A.id
Join B On C.b_id = B.id

Related

mysql merge query in one output with different fields

I have four db table,
tableA, tableB, tableC, tableD
I am listing records with join. These all working fine.
My query is like following.
Select DISTINCT A.*, B.loginname, B.email, C.full_name, C.address1, C.state, C.city, D.*
FROM (
tableA AS A
INNER JOIN tableB AS B ON B.user_id = A.id
INNER JOIN tableC AS C ON C.user_id = A.id
INNER JOIN tableD AS D ON D.user_id = A.id
) WHERE A.id = '269' ORDER BY A.created_date DESC
Now, issue is i have created four another table with almost same details but there are different fields and columns. So i can not use UNION.
tableE, tableF, tableG, tableH
i want to merge output to display. And output should be ORDER BY A.created_date OR ORDER BY E.created_date
Advanced thanks...
You can use UNION ALL (or UNION, if you want duplicates removed) between two tables with differently named columns by declaring name aliases.
SELECT 'first' AS source, a,b,c,d
FROM first
UNION ALL
SELECT 'second' AS source, q AS a, r AS b, s AS c, t AS d
FROM second
This will yield a result set (virtual table) with columns named source,a,b,c,d.

Postgresql query issue

so I have a problem that I'm trying to resolve since a couple of weeks, but I'm not coming to any solution. So here are the tables that I'm trying to make a query on:
Tables
I obviously joined them (easy):
SELECT a.date,b.title,b.author,u.nick
FROM book_add a
INNER JOIN user u on(a.user_fk=u.id)
INNER JOIN book b on (a.book_fk=b.id)
INNER JOIN status s ON(a.status_fk=s.id)
WHERE s.description='active';
Now here comes the problem: I want to order the rows by date desc and distinct them, so that the last inserted row (with the newest date) the first row is. But results are very odd once they get distincted. I tried this:
SELECT a.date,b.title,b.author,u.nick
FROM book_add a
INNER JOIN user u on(a.user_fk=u.id)
INNER JOIN book b on (a.book_fk=b.id)
INNER JOIN status s ON(a.status_fk=s.id)
WHERE s.description='active' ORDER BY a.date DESC;
this works, though once i try distinctig a.book_fk results are wrong:
SELECT DISTINCT ON(a.book_fk)a.book_fk,a.date,b.title,b.author,u.nick
FROM book_add a
INNER JOIN user u on(a.user_fk=u.id)
INNER JOIN book b on (a.book_fk=b.id)
INNER JOIN status s ON(a.status_fk=s.id)
WHERE s.description='active' ORDER BY a.date DESC;
I even tried approaches like this one, but without success:
SELECT * FROM (SELECT DISTINCT
ON(a.book_fk)a.book_fk,a.date,b.title,b.author,u.nick
FROM book_add a
INNER JOIN user u on(a.user_fk=u.id)
INNER JOIN book b on (a.book_fk=b.id)
INNER JOIN status s ON(a.status_fk=s.id)
WHERE s.description='active') res ORDER BY res.date DESC
Could someone help me? I would be very happy! Thank you!
It sounds like you're trying to get one row per book, with the most recent user/status? In that case this should work:
SELECT DISTINCT ON(a.book_fk)
a.book_fk, a.date, b.title, b.author, u.nick
FROM book_add a
INNER JOIN user u on(a.user_fk=u.id)
INNER JOIN book b on (a.book_fk=b.id)
INNER JOIN status s ON(a.status_fk=s.id)
WHERE s.description='active'
ORDER BY a.book_fk, a.date DESC
;

Which Join do i use for update when one of my table is empty for a common id?

I am using a left to update values in multiple table . I have three table a b and c . i have a common id for all three tables. But i need to update all the tables in one go even if one of the tables does not have a entry ? is it possible. which join do i use ?
You could use multiple LEFT JOINs with a GROUP BY:
SELECT *
FROM a
LEFT JOIN b ON a.id = b.id
LEFT JOIN c ON a.id = c.id
GROUP BY a.id

Mysql count joined fields

I have 3 tables: accounts, comments and followers
In account I track user info, in comments their comments and in followers, who are they following. I want to create a query where I get basic info of the account, a count of comments and a count of followers. I have a query and is not working:
SELECT
a.company,
a.firstname,
a.lastname,
a.title,
a.email,
a.zipcode,
a.created,
a.newsletter,
COUNT(c.id) comments,
COUNT(f.id) follows,
a.linkedinid
FROM accounts a
LEFT JOIN comments c ON a.id = c.user_id
LEFT JOIN followers f ON a.id = f.user_id
Any idea what am I doing wrong?
Think there are 2 issues.
Firstly you have no GROUP BY clause so it will bring back the counts for everything (one row returned), rather than one row with the counts per account. The account details would be from an undefined row.
Secondly you just COUNT(c.id) / COUNT(f.id). If an account has multiple follows and multiple comments then it will get every combination of those followers and accounts, so the counts would apply to the number of combinations. For example 2 comments and 3 followers would give 6 combinations, and both counts would be 6.
Fixing these by adding a GROUP BY and also adding DISTINCT to the COUNTs gives:-
SELECT
a.company,
a.firstname,
a.lastname,
a.title,
a.email,
a.zipcode,
a.created,
a.newsletter,
COUNT(DISTINCT c.id) comments,
COUNT(DISTINCT f.id) follows,
a.linkedinid
FROM accounts a
LEFT JOIN comments c ON a.id = c.user_id
LEFT JOIN followers f ON a.id = f.user_id
GROUP BY a.company,
a.firstname,
a.lastname,
a.title,
a.email,
a.zipcode,
a.created,
a.newsletter,
a.linkedinid
Note that in MySQL it is not necessary to GROUP BY all the non aggregate fields as long as the others are dependent on a field that in in the GROUP BY. However most other flavours of SQL will fail if you do this so to me it is best practice to put them all in the GROUP BY.
Try this
SELECT
a.company,
a.firstname,
a.lastname,
a.title,
a.email,
a.zipcode,
a.created,
a.newsletter,
COUNT(c.id) comments,
COUNT(f.id) follows,
a.linkedinid
FROM accounts a
LEFT JOIN comments c ON a.id = c.user_id
LEFT JOIN followers f ON a.id = f.user_id
GROUP BY f.user_id

LEFT JOIN two tables with the same column name and print them with PHP

I have two table in my MySQL database:
USERS ('id_user' - 'id_client' -> the same as the id in CLIENTS)
CLIENTS ('id_client' - 'name' etc.)
I want to print all the clients and the respective users. This is my query:
SELECT * FROM clients AS c LEFT JOIN users AS u ON c.id_client = u.id_client
It seems to be ok, but I am having trouble when I try to print the the id_client from the table clients. How can I print them using PHP? It seems they are empty... Is my query wrong?
Try,
Either GROUP BY or DISTINCT is needed
SELECT * FROM clients AS c LEFT JOIN users AS u ON c.id_client = u.id_client GROUP BY c.id_client
SELECT c.,u. FROM clients c LEFT outer JOIN users u ON c.id_client = u.id_client
Since you have field with the same name in both tables ("id_client"), you have to specify which one to be printed.
Like so:
SELECT u.id_client, c.id_client, ... FROM clients AS c LEFT JOIN users AS u ON c.id_client = u.id_client
Simpler than that, just do the following:
SELECT c.*, u.name FROM clients AS c LEFT JOIN users AS u ON c.id_client = u.id_client
This will select everything from table c, and only name from table u. The issue you are having is you are selecting id_client from both tables, which is not necessary, and causes confusion when trying to reference it with php.

Categories