How can I check two conditions before inserting? - php

I have three tables:
// Posts
+----+----------+---------------+-----------+
| id | title | content | id_author |
+----+----------+---------------+-----------+
| 1 | title1 | content1 | 1234 |
| 2 | title2 | content2 | 5678 |
+----+----------+---------------+-----------+
// Users
+----+--------+--------+
| id | name | active |
+----+--------+--------+
| 1 | jack | 1 |
| 2 | peter | 0 |
| 3 | John | 1 |
+----+--------+--------+
// Votes
+----+---------+---------+
| id | id_post | id_user |
+----+---------+---------+
| 1 | 32 | 1234 |
| 2 | 634 | 5678 |
| 3 | 352 | 1234 |
+----+---------+---------+
Now I need to check two conditions before inserting a new vote into Votes table:
The id of author and what I have passed are the same? Posts.id_user = :author (I know I can do that by a FK, but I don't want)
The account of current user is active? Users.active = 1
Also here is my query:
INSERT INTO Votes (id_post,id_user)
SELECT ?,?
FROM Posts p
WHERE p.id_user = :author limit 1;
How can I add second condition Users.active = 1 to my query?
EDIT: Actually I'm trying to don't let people be able to vote who are inactive (active = 0). For example if SO bans men, then I cannot vote to post anymore, because I (current user) am banned. So I'm pretty sure $_SESSION['id'] should be used in the query.

INSERT INTO Votes (id_post,id_user)
SELECT p.id,u.id
FROM Posts p, Users u
WHERE p.id_user = :author
AND u.id = :user
AND u.active = 1 limit 1;
then you set parameter user equal to the current user id.
EDIT: I suppose id_user in table Votes must be the voter's id, not the author of the post (correct?), so I fixed the query eliminating the JOIN.

Use and with where
INSERT INTO Votes (id_post,id_user)
SELECT ?,?
FROM Posts p, Users u
WHERE p.id_user = :author and u.active = 1 limit 1;

INSERT INTO Votes (id_post,id_user)
SELECT p.id, u.id
FROM Posts p
INNER JOIN Users u ON 1 = 1
WHERE p.id_user = :author
AND u.id = :current_user_id
AND u.active = 1
LIMIT 1;

Related

How can I restrict selecting to only rows which have common value?

Here is my table structure:
// users
+----+--------+
| id | name |
+----+--------+
| 1 | Jack |
| 2 | Peter |
| 3 | John |
| 4 | Barman |
| 5 | Ali |
+----+--------+
// friends
+---------+-----------+
| user_id | friend_id |
+---------+-----------+
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
| 3 | 1 |
| 3 | 2 |
| 3 | 4 |
| 5 | 2 |
+---------+-----------+
-- both user_id and friend_id columns refer to the id column of users table
And here is my query:
// $id = 1;
select distinct f1.user_id, f1.friend_id
from friend f1
where user_id = $id
or
user_id in (select f2.friend_id
from friend f2
where user_id = $id);
/* output
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
| 3 | 2 |
| 3 | 4 |
| 5 | 2 |
As you see, my query selects
Jack (because of $id = 1)
All Jack's friends
All friends of Jack's friends
Ok all fine. In reality, I'm trying to make a graph of the result. actually I did it. Now I want to restrict the results to only common friends. I mean I want to remove single nodes. In other word, I want to select friends who have at least two edges.
Is doing that possible by changing the query or should I do that in the PHP layer?
A visual example and its expected output:
The bellow query will return all the common friends that are not direct friends:
select distinct f.user_id, f2.friend_id common_friend
from friends f
inner join friends f2
on f.friend_id = f2.user_id
left join friends f3
on f.user_id = f3.user_id
and f2.friend_id = f3.friend_id
where f3.user_id is null
and f.user_id <> f2.friend_id
#and f.user_id = 1 Jack
The first 'inner' join returns the circle of friends and the second 'left' join joins the first-order friends from the circle -> the where f3.user_id is null removes the first-order friends.
The last f.user_id <> f2.friend_id is to remove a user being a common friend to himself.
Try this
SELECT DISTINCT f1.user_id, f1.friend_id
FROM friend f1
WHERE (
user_id = $id
OR user_id IN (
SELECT f2.friend_id
FROM friend f2
WHERE user_id = $id))
AND (friend_id IN (
SELECT f3.friend_id
FROM friend f3
WHERE user_id = $id))

mysql select where left join syntax

I have a problem. I have 2 database tables.
table 1 people:
+----------+--------------+
| id | name |
+----------+--------------+
| 1 | johanalj |
| 2 | hjgjhggjh |
+----------+--------------+
table 2 images of people:
+----------+--------------+----------------+
| id | url | people_ID |
+----------+--------------+----------------+
| 1 | 3765345.png | 1 |
| 2 | 87e58974.png | 1 |
+----------+--------------+----------------+
Now I want to select person with id 1 from table 1 and all pictures from table 2 that have people_ID 1.
I tried LEFT JOIN in combination with a WHERE but cant get it to work
$sql = "SELECT * FROM people p LEFT JOIN images i ON i.people_ID = p.id WHERE id = '1'";
But I get a no result massage. What am I doing wrong?
There is an error(ambiguous column id). Both tables have id column. You need to add the table alias with id. try with -
$sql = "SELECT * FROM people p LEFT JOIN images i ON i.people_ID = p.id WHERE p.id = '1'";

PHP MySQL Query With Linking Table

I want to ask you about Friend Suggestions facebook like.
I have 2 table, that's : tb_user and tb_friend.
tb_user
uid | username | full_name
1 | dean | Dean Cool
2 | ryanblk | Ryan Black
3 | maria | Maria Bellen
4 | greg | Greg Munch
tb_friend
friend_one_id | friend_two_id | status
1 | 2 | 1
1 | 4 | 1
3 | 4 | 1
For status :
1 --> Friend
Now I want to query tb_user linking with tb_friend. Condition, if never friend (Status is not 1) then show it into suggestions friend.
My SQL query so far :
$uid --> my session
SELECT
U.uid, U.username, U.full_name FROM tb_user U
WHERE
U.uid != '$uid' ORDER BY RAND() LIMIT 3
Please help!
I had a similar scenario where i used this as query string
$query = "SELECT * FROM friend_table,user_table
WHERE ((from_id = '".$_SESSION['logged_user_id']."' AND friend_table.to_id = user_table.user_id)
OR
(to_id = '".$_SESSION['logged_user_id']."' AND friend_table.from_id = user_table.user_id))
AND
friend_table.status <> '1'";
I am editing my db_structure so that you understand
[WAIT TILL I GIVE DB STRUCTURE SO THAT YOU UNDERSTAND. I AM EDITING THE ANSWER]
My db struture was like this
user_table
|-------------------------------------------------------------|
| user_id | username | first_name | last_name |
|-------------------------------------------------------------|
| 1 | smruti | Smruti | Singh |
|-------------------------------------------------------------|
| 2 | sunaina | Sunaina | Singh |
|-------------------------------------------------------------|
my friend_table to store the realtion between friends
|-------------------------------------------------------------|
| relation_id | from_id | to_id | status |
|-------------------------------------------------------------|
| 1 | 1 | 2 | 1 |
|-------------------------------------------------------------|
| 2 | 3 | 4 | 0 |
|-------------------------------------------------------------|
EDIT
This wil show all the result of users who are not your friend.
Keep one thing in mind, if you sent a request to someone then a row is inserted into the friend_table with status 0. If he/she accepts, the status becomes 1. If he/she rejects, the staus becomes 2. If status is 2, you can again send request to him/her.
So here the user_id who are either present in friend_table with status not equal to 1, or who are not present at all.
$query = "SELECT * FROM user_table WHERE user_id NOT IN
(
SELECT user_id FROM friend_table,user_table
WHERE ((from_id = '".$_SESSION['logged_user_id']."' AND
friend_table.to_id = user_table.user_id)
OR
(to_id = '".$_SESSION['logged_user_id']."' AND
friend_table.from_id = user_table.user_id))
AND
friend_table.status = '1')";

Check if records exists in join and assign and count it

Im making an application where its possible to vote on pictures.
Im doing some joins and would like to check each picture i get, if the user logged in has voted on it.
My votes table setup is like this:
+-------------------------+
| id | user_id | photo_id |
+-------------------------+
| 1 | 2 | 6 |
+-------------------------+
| 2 | 4 | 5 |
+-------------------------+
| 3 | 3 | 5 |
+-------------------------+
| 4 | 1 | 6 |
+-------------------------+
Im joining 3 tables:
users, photos and votes
SELECT
users.*,
photos.*,
users.id as user_userid,
photos.id as photo_photoid,
COUNT(votes.id) as totalvotes
FROM
photos
LEFT JOIN
votes
ON
votes.photo_id = photos.id
LEFT JOIN
users
ON
users.id = photos.author_id
GROUP BY
photos.id
ORDER BY
totalvotes
DESC
I would like to make a query inside this sql that does something like this:
+---------------------------------------------------------------------------------+
| photo_photoid | user_userid | totalVotes | currentUserHasVotetThisAmountOfTime |
+---------------------------------------------------------------------------------+
| 6 | 1 | 2 | 1 |
+---------------------------------------------------------------------------------+
| 5 | 1 | 2 | 0 |
+---------------------------------------------------------------------------------+
So i guess im looking for a count of the records, where votes.user_id = $MyLoggedInUser AND votes.photo_id = photo.id
Any suggestions?
Here is your request :
SELECT
v.photo_id,
COUNT(v.id) AS total_votes,
(SELECT COUNT(id) FROM vote WHERE photo_id = v.photo_id AND user_id = 1) AS currentUserHasVotetThisAmountOfTime
FROM
vote AS v
GROUP BY
v.photo_id
ORDER BY
total_votes
DESC
Just replace user_id = 1 by your own ID (in the sub-request line 4).
I get ride of the user_id column, since this is your something you provide there is imo no point to return this in the query as well.
If you want to test it by yourself : http://sqlfiddle.com/#!2/ba2a1/16/0

Following mySQL table search result

I have a following system where users can follow each other and search for users to follow. Here are examples of the users and following tables:
Users Table:
+-------+-----------+
| id | userName |
+-------+-----------+
| 1 | John |
| 2 | Harriet |
| 3 | Chris |
| 4 | Lisa |
| 5 | Joe |
+-------+-----------+
Following Table:
+-------+-------+-------+
| id | id_1 | id_2 |
+-------+-------+-------+
| 1 | 5 | 3 |
| 2 | 1 | 2 |
| 3 | 1 | 5 |
| 4 | 5 | 4 |
+-------+-------+-------+
I want to know how to structure a mySQL statement to join the following table with the users table where the username is like '%$search%' to return all results of this occurrence (i.e. searching 'jo' returns John and Joe) and when there are multiple results for each person (i.e. Joe will occur twice in Following) return only results where id_1 is the current logged in users id to show that the logged in user is already following this person.
$search = $_POST['search'];
$userid = $_POST['userid'];
$qry = "
SELECT u.id
, u.userName
, f.id_1
, f.id_2
FROM users u
LEFT
JOIN following f
ON IF('$userid' = f.id_1, u.id = f.id_2, u.id = f.id_1)
WHERE u.userName LIKE '%$search%';
";
This query returns every occurrence of the username in the following table but it should only return once.
Any help would be great.
Thanks.
Not absolutely sure but give it a try
SELECT u.id
, u.userName
, f.id_1
, f.id_2
FROM users u
LEFT JOIN following f
ON u.id in (f.id_1,f.id_2)
AND f.id_1 = '$userid'
WHERE u.userName LIKE '%$search%'
ORDER BY u.id
LIMIT 1;

Categories