php symfony doctrine or taking data from 2 tables - php

Since I'm very new don't judge me. I am trying to make a filter but there are some difficulties on the way. I have two tables of data. First with companies, second with users.
Companies:
____________________
id | name |
1 | try |
2 | test |
3 | experiment |
Users:
_____________________________________
|id | company_id | name | status |
| 1 | 1 | Idiot |pending |
| 2 | 1 | Funny |active |
| 3 | 2 | Me |pending |
| 4 | 2 | Lucky |rejected|
| 5 | 2 | Moon |rejected|
I have to make perhaps INNER JOIN and take only companies and users that are pending. I'm not interested in 'rejected'. So I'm interested to get:
3 | 2 | Me | pending | test
and other record with pending and no active. The company must have pending user and the same company must not have active.
SELECT *
FROM users u
INNER JOIN companies c
ON u.company_id = c.id
WHERE u.status = 'pending'
AND NOT EXISTS(SELECT u2.status
FROM users u2 ON u2.id = c.id
WHERE u2.status = 'pending')
or something like that was the SQL but I can't check it now. I want to make it Doctrine
e.g. $query = ..->innerJoin(..)->where... but can't make it. Any help please. Oh and how would this handle 100,000 records database for example? Is there a better way? Thank you.

in /lib/model/doctine/UsersTable.class.php:
class UsersTable extends Doctrine_Table
{
public function getAllCompanies($user_id) {
$q = $this->createQuery('u')
->where('u.istatus = ?','pending')
->leftJoin('u.Companies c WITH c.id = u.company_id')
->execute();
return $q;
}
}

Related

Mysql query string for photo sharing social app like Instagram

I am developing a photo sharing app platform. The app allows you to post a photo and others can like or rate the photo. Users can follow each other and see photos their 'followings' are sharing, just like instagram.
#user_tbl
id | name | number
-------------------
1 | Dan | 0209
2 | Sam | 2854
3 | Dave | 8123
4 | Alex | 5600
#photo_tbl
id | userid | path
-------------------
1 | 3 | dave-dog.jpg
2 | 1 | dans-cat.png
3 | 4 | alex-bird.jpg
4 | 2 | sam-fish.jpg
#friendship_tbl
id | actor | target
--------------------
1 | 2 | 1 // Sam is following Sam
2 | 2 | 4 // Sam is following Alex
3 | 1 | 3 // Dan is following Dave
4 | 4 | 2 // Alex is following Sam
#activities_stream_tbl
id | photoid | userid | context | date
----------------------------------------------------------
1 | 3 | 4 | add-new-photo | 10/10/2015
2 | 1 | 3 | add-new-photo | 12/10/2015
3 | 3 | 2 | Sam-share-Alex-photo | 15/10/2015
4 | 4 | 2 | add-new-photo | 20/10/2015
6 | 1 | 1 | Dan-like-Dave-photo | 21/10/2015
The #user_table holds the basic info of a user, while #photo_tbl hold the name and path of the photo shared by the user. In the #friendship_tbl is the relationship link between users. "actor" column is the id of the user doing the following while "target" column is the id of the user being followed.
I am currently having problem writing a query string to pull photos of USERX and photos of other users USERX is following and GROUP them by "photoid" in the activities_stream_tbl and ORDER BY "date" activities_stream_tbl.
I will be glad if anyone can help me, show me a better way of structuring db thank you.
to pull photos of USERX, you can construct your sql like
select PATH
from user_tbl as a inner join photo_tbl as b
on a.id = b.user_id
and a.name = 'userx'
and to pull photos of other users USERX is following, you may write
select path
from photo_tbl as a
where a.userid in (select target from friendship_tbl as x inner join user_tbl as y on x.actor = y.id and y.name = 'user')
you can union the above two results if you want.
ex:
select PATH
from user_tbl as a inner join photo_tbl as b
on a.id = b.user_id
and a.name = 'userx'
UNION
select path
from photo_tbl as a
where a.userid in (select target
from friendship_tbl as x
inner join user_tbl as y
on x.actor = y.id and y.name = 'user')

JOIN multiple MySQL tables in query

I'm not sure if this is even possible, or if my JOIN-fu just isn't strong enough(it's pretty wimpy to tell you the truth). I have 3 tables which are tied together with a UID. I'm trying to get information from a result from all of them into one query, except I'm having trouble with making sure the result is what I want.
users
========= USERS ==========
| uid | nickname |
--------------------------
| testusr1 | Test User 1 |
| testusr2 | Test User 2 |
| testusr3 | Test User 3 |
| testusr4 | Test User 4 |
============= GALLERY ===========
| id | uid | ext | profile |
---------------------------------
| 1 | testusr1 | png | 1 |
| 2 | testusr2 | jpg | 1 |
| 3 | testusr3 | png | 1 |
| 4 | testusr4 | png | 1 |
| 5 | testusr4 | jpg | 0 |
============= FRIENDS =============
| sender | reciever | status |
-----------------------------------
| testusr1 | testusr3 | 0 |
| testusr2 | testusr3 | 1 |
| testusr2 | testusr1 | 1 |
| testusr3 | testusr4 | 1 |
What I'm trying to do is get all of a user's friends. Friends are in the friends table where the status = 1. The uid can be either the sender or the reciever. In the table above, testusr3's friends are: testusr2 and testusr4
From here I want to snag the nickname from users, and the id from gallery WHERE profile = 1 AND uid = (that friend's ID).
So far, my query looks like:
$query = "SELECT u.uid AS USERID, g.id, g.ext, f.sender, f.reciever
FROM friends f
LEFT JOIN gallery g ON g.uid = f.sender AND g.profile = 1
LEFT JOIN users u ON u.uid = f.sender
WHERE f.status = 1
AND (f.sender = '$sentuid' OR f.reciever = '$sentuid')";
But, it labels all of the results as f.sender...and I'm pretty sure the g.profile = 1 isn't working. It does grab the friends accurately though. Any help would be greatly appreciated.
Untested Solution
Best place to start is to get the matching records in a single column, with UNION. Then you have all the UIDs you need, in one place.
SELECT f.uid, u.nickname, g.id
FROM
(
(SELECT reciever as uid FROM friends where status=1 and sender='$sentuid')
UNION
(SELECT sender as uid FROM friends where status=1 and reciever='$sentuid')
) f
LEFT JOIN gallery g ON f.uid = g.uid and profile=1
LEFT Join users u ON f.uid = u.uid
Side notes:
Generally a bad idea to use char for an ID field.
For performance reasons, you may be better off actually using more storage space, and doubling up on the 'friends' records. i.e.: two entries for each friendship.
Seems to me that you're really close, but a SQLfiddle would help. I believe you are right, g.profile = 1 is not working because you have no table reference for it, might as well take it out. But because of the join, you should be able to select it.
$query = "SELECT u.uid AS USERID, g.id, g.ext, g.profile, f.sender, f.reciever
FROM friends f
LEFT JOIN gallery g ON g.uid = f.sender
RIGHT JOIN users u ON u.uid = f.sender
WHERE f.status = 1
AND (f.sender = '$sentuid' OR f.reciever = '$sentuid')";

Performing Searches over Many-to-Many Relationships

I have a database which (for the purposes of this example), has two tables that have a many to many association (with an intermediary table for holding the associations). Here is there structure:
Table A:
+-----+-------+-------+-------+
| aID | aCol1 | aCol2 | aCol3 |
+-----+-------+-------+-------+
| 1 | foo | aoo | doo |
+-----+-------+-------+-------+
| 2 | bar | aar | dar |
+-----+-------+-------+-------+
| 3 | baz | aaz | daz |
+-----+-------+-------+-------+
Table B:
+-----+-------+
| bID | bCol1 |
+-----+-------+
| 1 | alice |
+-----+-------+
| 2 | bob |
+-----+-------+
Association Table:
+-----+-----+
| aID | bID |
+-----+-----+
| 1 | 1 |
+-----+-----+
| 2 | 2 |
+-----+-----+
| 3 | 1 |
+-----+-----+
If I want to search for information by aCol2 LIKE 'aa%' AND the row has an association to bCol1 = 'bob' (i.e. resulting in only row aID = 2), how could I assemble a MySQL Query that could do something similar?
p.s. Sorry for the poor clarity, I am not exactly sure of the wording, but in a nut shell, it is about searching for data from one record that (for the purposes of this) has a 1-* relationship via a connecting table to a number of records, by information that exists in the entire set
SELECT
a.*
FROM
table_b b
INNER JOIN associations ab ON (b.b_id = ab.b_id)
INNER JOIN table_a a ON (ab.a_id = a.a_id)
WHERE
b.col_1 = 'bob'
AND a.col_2 LIKE 'aa%'
It's been awhile, but I believe this should work:
SELECT
*
FROM
A,
B,
associations
WHERE
A.aCol2 LIKE 'aa%' AND
A.aID = associations.aID AND
associations.bID = B.bID
You have to do 2 inner joins to combine the 3 tables.

Selecting multiple tables MySQL and retrieving different data

I have 4 tables that I need to pull data from. I need to count how many people are signed for a single event and see if a user is applied for an event.
These are my table setups:
TABLE: users
+----+----------+-------+--------+-------+
| id | username | level | class | guild |
+----+----------+-------+--------+-------+
| 1 | example1 | 100 | Hunter | blah |
| 2 | example2 | 105 | Mage | blah2 |
| 3 | example3 | 102 | Healer | blah |
+----+----------+-------+--------+-------+
ID is primary
TABLE: event_randoms
+----+----------+-------+--------+----------+----------+
| id | username | level | class | apped_by | event_id |
+----+----------+-------+--------+----------+----------+
| 1 | random1 | 153 | Hunter | 3 | 3 |
| 2 | random2 | 158 | Healer | 3 | 1 |
| 3 | random3 | 167 | Warrior| 1 | 3 |
+----+----------+-------+--------+----------+----------+
ID is primary
apped_by should be foreign key to users.id
event_id should be foreign key to events.id
TABLE: events
+----+------------+------------+-----------+-----------+-----------+
| id | event_name | event_date | initiator | min_level | max_level |
+----+------------+------------+-----------+-----------+-----------+
| 1 | event1 | date1 | 1 | 100 | 120 |
| 2 | event2 | date2 | 1 | 121 | 135 |
| 3 | event3 | date3 | 1 | 100 | 120 |
| 4 | event4 | date4 | 1 | 150 | 200 |
+----+------------+------------+-----------+-----------+-----------+
ID is primary
TABLE: event_apps
+----+----------+--------------+
| id | event_id | applicant_id |
+----+----------+--------------+
| 1 | 3 | 2 |
| 2 | 4 | 2 |
| 3 | 3 | 1 |
| 4 | 1 | 3 |
+----+----------+--------------+
ID is primary
event_id should be foreign key to events.id
applicant_id should be foreign key to users.id
I will be the first to admit that I am very new to this. I just learned how to use MySQL a few days ago. I can grab stuff from a single table, but I am unsure how to grab from multiple tables.
This is the SQL query I tried
SELECT DD_events.id, event_id, applicant_id, guild, level, class, DD_users.id
FROM DD_events, DD_event_apps, DD_users
WHERE DD_event_apps.event_id = DD_events.id
AND DD_event_apps.applicant_id = DD_users.id
and tried to print_r an array but the array turns up empty.
So a few questions pertain to this:
1: How would I count and display as a number how many people (users and randoms) are signed up for an event?
eg: event 3 should have 4 total (2 users and 2 randoms)
2: How do I see if a particular individual is signed for an event and display text based if they are or not?
eg: user 1 is signed up for event 3 so it would be "Registered" but user 2, who is not signed, would display "Not Registered"
3: I want to display info for who is signed for a particular event in 2 tables, 1 for users and another for randoms.
eg: Event 3 would have 2 users info (username, guild, class, level) under the users table and then 2 random users info (name, class, level, what user applied this person) in the random table.
Any and all help is appreciated even if you can answer 1 part.
I'm thinking this would be your base query:
SELECT
event.id,
app.applicant_id,
usr.guild,
usr.level,
usr.class,
usr.id AS Userid
FROM
DD_events event
JOIN
DD_event_apps app
ON (event.id = app.event_id)
LEFT JOIN
DD_users usr
ON (app.user_id = usr.id)
You can make modifications to this to aggregate it, like so:
SELECT
event.id,
COUNT(app.applicant_id) AS ApplicantCount,
COUNT(DISTINCT usr.guild) AS UniqueGuilds,
COUNT(DISTINCT usr.level) AS UniqueLevels,
COUNT(DISTINCT usr.class) AS UniqueClasses,
COUNT(DISTINCT usr.id) AS UniqueUsers
FROM
DD_events event
JOIN
DD_event_apps app
ON (event.id = app.event_id)
LEFT JOIN
DD_users usr
ON (app.user_id = usr.id)
GROUP BY
event.id
I could write those scripts for you, but I think this provides a good starting point for you to continue from. You'll find that T-SQL is fairly simple when you are trying to get the results you are looking for. Hope this helps!
<?php $query = "SELECT count(*) AS numbuh FROM DD_event_apps WHERE event_id = {$row['id']}";
try
{
// These two statements run the query against your database table.
$stmt = $db->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code.
die("Failed to run query: " . $ex->getMessage());
}
echo($query);
// Finally, we can retrieve all of the found rows into an array using fetchAll
$count = $stmt->fetchAll();
echo($count['numbuh']); ?>

mySQL: Showing data from one table using id from another

I want my users to be able to make a favourite list.
I have two tables in a database in mySQL. One stores information about businesses and the other stores the unique user ids as well as the ids from the first table that the user has marked a favourite.
Table 1
<pre>
ID | NAME | EMAIL | PHONE |
1 | Joe | a#mail.com | 25634565 |
2 | John | b#mail.com | 43634565 |
3 | Jack | c#mail.com | 65634565 |
4 | James| d#mail.com | 43634565 |
5 | Julie| e#mail.com | 65634565 |
...
</pre>
Table 2
<pre>
USERID | FAV1 | FAV2 | FAV3 | FAV...
2565325489 | 1 | 3 | 5 |
8596854785 | 3 | 2 | NULL |
2356256263 | 5 | NULL | NULL |
...
</pre>
The output I want for a user (in this example the first in table2):
<pre>
Joe | a#mail.com | 25634565 |
Jack | c#mail.com | 65634565 |
Julie| e#mail.com | 65634565 |
</pre>
I have looked into JOIN LEFT and minus query calls, but I just can't make it work. I have a basic understanding of mySQL and PHP, but not a lot.
I would highly appreciate any help with what approach to take.
Ps. If there are better ways to structures my databases, I would love to know.
I'd use a table with two fields - userID and fav - make one entry for each entry. And then...
SELECT table1.name, table1.email, table1.phone FROM table1,table2 WHERE table2.fav = table1.id AND table2.userid = 2565325489
Select * from table1 InnerJoin (Select * from table2) as t4 on table1.ID=t4.FAV1
$result = mysqli_query('SELECT name,email,phone FROM t1 table1 LEFT JOIN table2 t2 ON t1.ID = t2.fav1');
//iterate the results
while ($row = mysqli_fetch_array($result))
{
echo $row['name']." ".$row['email']." "$row['phone'];
}

Categories