I cant write the correct SQL query correcty - mysql

I have 2 table in the database and ı am trying to filter some users according to some criterias.
criterias table
+---------+----------------------+--------------------+----------------------+
| user_id | searching_friendship | searching_practice | conversation_subject |
+---------+----------------------+--------------------+----------------------+
| 31 | | | science |
| 26 | on | on | love |
| 32 | on | off | science |
| 34 | | | |
+---------+----------------------+--------------------+----------------------+
user table
+---------+------------+-----------------------------+---------------+-------------+---------------+--------------+------------------+
| user_id | user_name | user_email | user_password | user_gender | user_language | language_lvl | last_login |
+---------+------------+-----------------------------+---------------+-------------+---------------+--------------+------------------+
| 26 | Furkan | furkanakgun#windowslive.com | 123 | | Türkiye | basic | 16.06.2019 11:57 |
| 31 | sfsdf | asdfasdf#hotmail.com | 123 | Male | Afrikaans | basic | 09.06.2019 20:01 |
| 32 | denemeuser | xxx#hotmail.com | 123 | Male | Amharic | intermediate | 16.06.2019 11:57 |
| 33 | Smith | ssdf | 123 | male | ing | upper | NULL |
| 34 | luser | llll#hotmai.com | 123456 | Male | Afrikaans | basic | 16.06.2019 10:32 |
+---------+------------+-----------------------------+---------------+-------------+---------------+--------------+------------------+
I am trying to match users who have same criterias.What i do is
$userId=$_SESSION['userId'];
$sql="SELECT* FROM criterias WHERE user_id='$userId'";
$query=mysqli_query($conn,$sql);
while($result=mysqli_fetch_assoc($query)){
$friendshipCheck=$result['searching_friendship'];
$pracCheck=$result['searching_practice'];
$conversationSub=$result['conversation_subject'];
}
so I am getting current criterias informations and ı am trying to match with other users like this
SELECT* FROM users,criterias WHERE (users.user_id=criterias.user_id AND users.user_id!='$userId') AND criterias.searching_friendship='$friendshipCheck' OR criterias.searching_practice='$pracCheck'
But it doesnt work. Do you have any idea how to fix this dear friends ?

If you want to get only the users with the same criteria as yours this query should work
SELECT *
FROM users AS u
INNER JOIN criterias AS c ON u.user_id = c.user_id
WHERE u.user_id != '$userId'
AND c.searching_friendship = '$friendshipCheck'
AND c.searching_practice = '$pracCheck'
AND c.conversation_subject = '$conversation_subject'

Providing the expected results vs what you are getting would help proving the best solution.
Here, I am trying to write the query based on the understanding of what you have tried to write in your code:
;WITH CTE AS
(
SELECT C.*
FROM Criterias C
WHERE C.user_id = <user_id_input>
)
SELECT *
FROM User U
INNER JOIN Criterias C
ON U.user_id = C.user_id
WHERE EXISTS (
SELECT 1
FROM CTE
WHERE CTE.searching_friendship = C.searching_friendship
OR CTE.searching_practice = C.searching_practice
)
AND U.user_id != <input_user_id> ;

Related

SQL Query: How to get data from three different tables

I am using mysql and here is the schema that I have.
First Table: Domains
+-----------+--------------------+---------------+
| domain_id | domain_name | campaign_name |
+-----------+--------------------+---------------+
| 1 | test.org | campaign 1 |
| 2 | example.org | campaign 2 |
+-----------+--------------------+---------------+
Second Table: Users
+---------+-----------------+---------------+
| user_id | first_ame | last_name |
+---------+-----------------+---------------+
| 1 | John | Zimmer |
| 2 | Brian | Roberts |
| 3 | Jon | McNeill |
| 4 | Chris | Lambert |
| 5 | Vipul | Patel |
| 6 | Logan | Green |
+---------+-----------------+---------------+
Third Table: Emails
+----------+----------------------------------+-----------+---------+
| email_id | email | domain_id | user_id |
+----------+----------------------------------+-----------+---------+
| 1 | b1#test.org | 1 | 2 |
| 2 | b2#test.org | 1 | 1 |
| 3 | a1#example.org | 2 | 2 |
| 4 | a2#example.org | 2 | 3 |
| 5 | a3#example.org | 2 | 3 |
| 6 | a4#example.org | 2 | 4 |
+----------+----------------------------------+-----------+---------+
I want to get first_name, last_name and email of specific campaign i-e campaign 2 as shown follow.
Here is Online DB Query Editor
Kindly guide me how can I write SQL query to accomplish that. Thanks
If I am not missing anything, this is basically a join. You have the ids nicely matched between the tables, so you can do:
SELECT u.*, e.email, d.campaign_name
FROM Users u JOIN
Emails e
ON u.user_id = e.user_id JOIN
Domains d
ON e.domain_id = d.domain_id
WHERE d.campaign_name = 'campaign 2';
The email table is a so called bridge table between the other two. You have to perform a join between the three tables:
SELECT first_name, last_name, email
FROM Domains JOIN Emails ON Domains.domain_id = Emails.domain_id JOIN Users ON Emails.user_id = Users.user_id
WHERE Domains.campaign_name = ...
SELECT first_name as 'First Name',last_name as 'Last Name', email as 'email ID',campaign_name as 'Compaign Name'
FROM Users u
inner join Emails e
on e.user_id = u.user_id
inner join Domains d
on d.domain_id = e.domain_id

Join 3 Tables in a MySql Query

i have this 3 tables in a MySql Database.
users
+----+------+--------+------+
| Id | Name | Status | Role |
+----+------+--------+------+
| 1 | A | Aktiv | Op |
| 2 | B | Aktiv | Op |
| 3 | C | Aktiv | Op |
| 4 | D | Aktiv | Op |
+----+------+--------+------+
cnt
+----+------+------------+------+
| Id | Name | Date | Type |
+----+------+------------+------+
| 1 | A | 2017-11-09 | Web |
| 2 | B | 2017-11-09 | Web |
| 3 | C | 2017-11-09 | Web |
| 4 | C | 2017-11-09 | Inb |
| 5 | A | 2017-11-09 | Web |
+----+------+------------+------+
Lead
+----+------+------------------+------------+
| Id | Name | Date | Type |
+----+------+------------------+------------+
| 1 | A | 2017-11-09 00:24 | Imported |
| 2 | B | 2017-11-09 09:32 | Activation |
| 3 | B | 2017-11-09 10:56 | Activation |
| 4 | D | 2017-11-09 12:21 | Activation |
| 5 | D | 2017-11-10 12:22 | Activation |
+----+------+------------------+------------+
I'm trying to join them in a main table but with no success, the query i'm using is:
SELECT IFNULL(u.Name,'Total') as "Name",
Sum(IF(c.Type = 'Web',1,0)) as "Cnt Web",
Sum(IF(l.Type = 'Activation',1,0)) as "Lead Web"
FROM users u
LEFT JOIN cnt c ON u.Name = c.Name and c.Date = '2017-11-09'
LEFT JOIN lead l ON u.Name = l.Name and l.Date>= '2017-11-09' AND l.Date< '2017-11-10'
WHERE u.Status = 'Aktiv' AND u.Role = 'Op'
GROUP BY u.Name WITH ROLLUP
The result i need is a table like this:
+----+------+--------+---------+
| Id | Name | Cnt Web| Lead Web|
+----+------+------------------+
| 1 | A | 2 | 0 |
| 2 | B | 1 | 2 |
| 3 | C | 1 | 0 |
| 4 | D | 0 | 1 |
+----+------+------------------+
When i try to join the first table with the second or the first with the third, i get the correct result, but i can't get the needed result when i join them all.
Any answer is the most welcomed. Thank you in advance.
Here's a solution using correlated sub-queries
SELECT u.Id,
u.Name,
(SELECT COUNT(Name) FROM cnt WHERE Name = u.name AND type = 'Web' AND Date = '2017-11-09') AS cnt_web,
(SELECT COUNT(Name) FROM lead WHERE Name = u.name AND type = 'Activation' AND Date>= '2017-11-09' AND Date< '2017-11-10') AS cnt_lead
FROM users u
WHERE u.Status = 'Aktiv' AND u.Role = 'Op'

SQL Join tablese multiple times on different columns

I've got two tables:
personal
____________
| id | name |
| 15 | Mike |
| 23 | Rich |
| 35 | Hugo |
and
events
___________________________
| id | driver | translator |
| 22 | 15 | 23 |
| 23 | 35 | 35 |
is there a way to join these two tables to get something like
events
___________________________________
| id | driverName | translatorName |
| 22 | Mike | Rich |
| 23 | Hugo | Hugo |
thx
you can try this
SELECT
e.id
,p1.name driverName
,p2.name translatorName
FROM `events` e
JOIN `personal` p1
ON p1.id=e.driver
JOIN `personal` p2
ON p2.id=e.translator
This will work
select e.id, (select name from Personal where id = e.driver) as DiverName,
p.name as TranslatorName
from Personal p
inner join [events] e on p.id = e.translator

Get all users even if no records in another table

I'm facing a problem here :
I have two tables :
A users table :
+----+---------------+----------+
| id | username | company |
+----±---------------±----------+
| 1 | John | 0 |
| 2 | Jack | 0 |
| 3 | Casimir | 0 |
±----±---------------±----------±
A orders table :
+----+---------------+----------+--------+
| id | date | iduser | status |
+----±---------------±----------+--------+
| 1 | 2012-05-28 | 1 | 1 |
| 2 | 2012-05-25 | 1 | 1 |
| 3 | 2012-04-28 | 2 | 1 |
| 4 | 2012-03-28 | 1 | 1 |
| 5 | 2012-02-28 | 2 | 0 |
±----±---------------±----------±--------+
What I'm trying to do is to get a result like this :
+----------+---------------+-------------+
| username | COUNT(order) | MAX(date) |
+----------±---------------±-------------+
| John | 3 | 2012-05-28 |
| Jack | 1 | 2012-04-28 |
| Casimir | 0 | NULL |
±----------±---------------±-------------±
Here's the request I have for the moment :
SELECT u.username, COUNT(o.id), MAX(o.date)
FROM users u
INNER JOIN orders ON u.id = o.iduser
WHERE o.status = 1
GROUP BY u.id
This request gives me a result like :
+----------+---------------+-------------+
| username | COUNT(order) | MAX(date) |
+----------±---------------±-------------+
| John | 3 | 2012-05-28 |
| Jack | 1 | 2012-04-28 |
±----------±---------------±-------------±
As you can see, the user Casimir is not shown as he made no order. How can I modify my request to get the result I need please ?
Thanks !
A LEFT JOIN or LEFT OUTER JOIN will include all rows of the inital table, including those where there is no match in the joined-to table
SELECT u.username, COUNT(o.id), MAX(o.date)
FROM users u
LEFT OUTER JOIN orders o ON u.id = o.iduser AND o.status = 1
GROUP BY u.id
You need to use an OUTER JOIN instead of your current INNER JOIN.
Have a look at Jeff's post here to see how they differ:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

SQL - How to link two tables with a third table and get all records

Okay, so these are my three tables:
USER
|--------+----------+----------|
| UserID | UserName | IsActive |
|--------+----------+----------|
| 10 | Mike | 1 |
| 11 | John | 1 |
| 12 | Beth | 1 |
|--------+----------+----------|
REPORT_DISTRIB (Linking Table)
|-----------+--------+----------+---------------|
| DistribID | UserID | ReportID | DistribToUser |
|-----------+--------+----------+---------------|
| 1 | 10 | 50 | 1 |
| 2 | 12 | 52 | 0 |
| 3 | 14 | 54 | 1 |
|-----------+--------+----------+---------------|
REPORT
|----------+------------+---------------|
| ReportID | ReportName | Distributable |
|----------+------------+---------------|
| 50 | FY2010 | 1 |
| 51 | FY2011 | 1 |
| 52 | FY2012 | 1 |
|----------+------------+---------------|
In the problem I'm facing, I have 200 users from the USER table that are active and 10 reports from the REPORT table that are distributable. For every user I need to display the User Name, the Report Name, and whether that report should be distributed to the user, like so:
|----------+------------+---------------|
| UserName | ReportName | DistribToUser |
|----------+------------+---------------|
| Mike | FY2010 | 1 |
| Beth | FY2012 | 0 |
|----------+------------+---------------|
What I'm trying to achieve is a list of 2000 results (200 users x 10 reports). The problem is that the REPORT_DISTRIB linking table doesn't have a record for every user with each report. I still feel like this should be possible... am I wrong in my thinking? Any help is greatly appreciated.
It's rough, but this is my query so far (which returns 1790 results):
SELECT u.UserName, r.ReportName, rd.DistribToUser
FROM USER u
LEFT JOIN REPORT_DISTRIB rd on rd.UserID = u.UserID
and rd.ReportID in (select r.ReportID from REPORT r where r.Distributable = 1)
OUTER APPLY (select r.ReportName from REPORT r where r.ReportID = rd.ReportID) r
WHERE u.IsActive = 1
SELECT u.UserName, r.ReportName, COALESCE(rd.DistribToUser, 0)
FROM USER u
CROSS JOIN REPORT r
LEFT JOIN REPORT_DISTRIB rd
ON u.UserID = rd.UserID
AND r.ReportID = rd.ReportID
WHERE u.IsActive = 1
AND r.Distributable = 1