I have 3 table
Table news:
id_post | news | id_user
3 | IT news | 1
4 | game news | 2
Table user:
id_user | username
1 | bocah
2 | gundul
And Table vote
id_vote | id_post | id_user | LIKE
10 | 3 | 2 | 1
And this is my sql query:
SELECT post.*, username, like, SUM(vote.like) AS like FROM post
INNER JOIN user ON post.id_user=user.id_user
INNER JOIN vote ON post.id_post=vote.id_post
WHERE
(`title` LIKE '%$word%' OR `username` LIKE '%$word%') AND post.id_user=user.id
LIMIT 15
I just want to create search form from searching post or user based on keyword. Then display post, user's username which is also the author of post and total like in that news.
The problem is when keyword not match with any post or any user, my expectation, it should return an empty row. But it's not, it's return 1 row with NULL value.
Any answer to solve this?
Apart from getting one row when there is no match, you probably also get one row when there are multiple matches. I think this is because you have an aggregation (sum) without having a group by.
If I'm correct, adding a group by clause should solve the problem:
SELECT post.*, username, like, SUM(vote.like) AS like FROM post
INNER JOIN user ON post.id_user=user.id_user
INNER JOIN vote ON post.id_post=vote.id_post
WHERE
(`title` LIKE '%$word%' OR `username` LIKE '%$word%') AND post.id_user=user.id
GROUP BY post.id /* Or what's its name */
LIMIT 15
It will then, however, return results by post, so if you search for a user, you will still get all their posts (that is, the top 15), but my guess is that that's exactly what you want.
Related
Following example: In my database I have two tables: One that stores user posts and their content and another that stores the likes of other users from these posts. If a user likes a post a new row gets inserted into the likes table.
If I make a SELECT call on the posts table it also returns the number of likes of the respective post using subqueries (SQL Fiddle):
SELECT allPosts.*,
(SELECT COUNT(*) FROM likes WHERE likes.postID = allPosts.id) AS likeCount
FROM posts allPosts
ORDER BY likeCount DESC
LIMIT 3;
| id | content | likeCount |
|----|---------|-----------|
| 2 | Post 2 | 2 |
| 3 | Post 3 | 1 |
| 1 | Post 1 | 0 |
Problem: If, for example, you want to sort by likeCount using ORDER BY, likeCount must also be generated for each individual post at the same time, which is particularly problematic for very large tables with several thousands of posts. For example, it happened to me that a call took up to 10 seconds for a table containing about 2000 posts, which of course is too slow.
How can you solve this problem? How can I sort by likeCount without having to query likeCount for each individual post, but still sort the posts based on their number of likes?
I am grateful for any help!
Correlated subquery could be rewritten as JOIN:
SELECT allPosts.id, allPosts.content,
COUNT(likes.postID) AS likeCount
FROM posts allPosts
LEFT JOIN likes
ON likes.postID = allPosts.id
GROUP BY allPosts.id, allPosts.content
ORDER BY likeCount DESC
LIMIT 3;
SQLFiddle demo
I currently have a web app where users can share posts and connect with other users. I store each post in a table named "posts" and all of the relationships between users in a table called "relations"
The structure of "posts" is:
+-------------+-----------+---------+------------+----------------+
| post_id | user_id | text | date | privacy |
+-------------+-----------+---------+------------+----------------+
| 1 | 4 | Hello | 1/13/2014 | 2 |
+-------------+-----------+---------+------------+----------------+
Privacy can either be 1 or 2
The structure of "relations" is:
+-------------+-----------+------------+------------+
|rel_id | sender | recipient | status |
+-------------+-----------+------------+------------+
| 1 | 17 | 4 | 1 |
+-------------+-----------+------------+------------+
Status can either be 1 or 2
Now, I want to have a "News Feed" like page where the user can see all of the posts from the people they are either friends with (status= 2) or following (status= 1). But I am having trouble with the query. I know how to do simple select queries, but I don't think that is possible with this.
So, I would like to select all of the posts from the "posts" table where the 'user_id' is the same as 'recipient' in the "relations" table where also the sender equals '17' (I am going to use a variable). Now on top of that, if the status of that row from "relations" is '1' and the 'privacy' from the "posts" row is '2', then skip that post.
How would I write this query?
Use joins
SELECT * FROM `posts`
join `relations` on `recipient` = `user_id`
WHERE `status` = 2
Use joins and where clauses, as follows:
SELECT *
FROM posts p
JOIN relations r ON p.user_id = r.recipient
WHERE (r.status = 1 OR r.status = 2)
AND (r.status != 1 OR p.privacy != 2);
For succinctness, it helps to alias the tables (eg "posts p") so that you can subsequently refer to fields from each of them specifically (eg "p.privacy").
This will join the tables, including any where relations.status is 1 or 2, yet skipping any where both relations.status is 1 and posts.privacy is 2.
Right now I'm working on expanding my website to new functionality. I want to enable notifications from different sources. Similar to groups and people on facebook. Here is my table layout right now.
course_updates
id | CRN (id of course) | update_id
------------------------------------
courses
id | course_name | course_subject | course_number
-------------------------------------------------
users
id | name | facebook_name
---------------------------------------------------
user_updates
id | user_id | update_id
------------------------
updates
id | timestamp | updateObj
---------------------------
What I would like to be able to do is take course_update and user_updates in one query and join them with updates along with the correct information for the tables. So for course_updates i would want course_name, course_subject, etc. and for user_updates i would want the username and facebook name. This honestly probably belongs in two separate queries, but I would like to arrange everything by the timestamp of the updates table, and I feel like sorting everything in php would be inefficient. What is the best way to do this? I would need a way to distinguish between notification types if i were to use something like a union because user_updates and course_updates can store a reference to the same column in updates. Any ideas?
You might not need updates table at all. You can include timestamp columns to course_updates and user_updates tables
CREATE TABLE course_updates
(
`id` int,
`CRN` int,
`timestamp` datetime -- or timestamp type
);
CREATE TABLE user_updates
(
`id` int,
`user_id` int,
`timestamp` datetime -- or timestamp type
);
Now to get an ordered and column-wise unified resultset of all updates you might find it convenient to pack update details for each update type in a delimited string (using CONCAT_WS()) in one column (let's call it details), inject a column to distinguish an update type (lets call it obj_type) and use UNION ALL
SELECT 'C' obj_type, u.id, u.timestamp,
CONCAT_WS('|',
c.id,
c.course_name,
c.course_subject,
c.course_number) details
FROM course_updates u JOIN courses c
ON u.CRN = c.id
UNION ALL
SELECT 'U' obj_type, u.id, u.timestamp,
CONCAT_WS('|',
s.id,
s.name,
s.facebook_name) details
FROM user_updates u JOIN users s
ON u.user_id = u.id
ORDER BY timestamp DESC
Sample output:
| OBJ_TYPE | ID | TIMESTAMP | DETAILS |
-------------------------------------------------------------------------
| C | 3 | July, 30 2013 22:00:00+0000 | 3|Course3|Subject3|1414 |
| U | 2 | July, 11 2013 14:00:00+0000 | 1|Name1|FB Name1 |
| U | 2 | July, 11 2013 14:00:00+0000 | 3|Name3|FB Name3 |
...
Here is SQLFiddle demo
You can then easily explode details values while you iterate over the resultset in php.
I don't think you should mix both of those concepts (user and course) together in a query. They have different number of columns and relate to different concepts.
I think you really should use two queries. One for users and one for courses.
SELECT courses.course_name, courses.course_subject, courses.course_number,
updates.updateObj,updates.timestamp
FROM courses, updates, course_updates
WHERE courses.id = course_updates.course_id
AND course_updates.udpate_id = updates.id
ORDER BY updates.timestamp;
SELECT users.name,users.facebook_name,updates.updateObj,updates.timestamp
FROM users ,updates, user_updates
WHERE users.id = user_updates.user_id
AND user_updates.update_id = updates.id
ORDER BY updates.timestamp;
If you are going to merge the two table you need to keep in mind 2 things:
Number of columns should ideally be the same
There should be a way to distinguish the source of the data.
Here is one way you could do this:
SELECT * FROM
(SELECT courses.course_name as name, courses.course_subject as details,
updates.updateObj as updateObj, updates.timestamp as timestamp,
"course" as type
FROM courses, updates, course_updates
WHERE courses.id = course_updates.course_id
AND course_updates.udpate_id = updates.id)
UNION ALL
SELECT users.name as name,users.facebook_name as details,
updates.updateObj as updateObj,updates.timestamp as timestamp,
"user" as type
FROM users ,updates, user_updates
WHERE users.id = user_updates.user_id
AND user_updates.update_id = updates.id) as out_table
ORDER BY out_table.timestamp DESC
The type will let you distinguish between user and course updates and could be used by your front end to differently colour the rows. The course_id does not appear in this but you can add it, just keep in mind that you will have to add some dummy text to the user select statement to ensure both queries return the same number of rows. Note that in case there is an update referring to both user and course, it will appear twice.
You could also order by type to differentiate user and course data.
I need to count the number of duplicate emails in a mysql database, but without counting the first one (considered the original). In this table, the query result should be the single value "3" (2 duplicate x#q.com plus 1 duplicate f#q.com).
TABLE
ID | Name | Email
1 | Mike | x#q.com
2 | Peter | p#q.com
3 | Mike | x#q.com
4 | Mike | x#q.com
5 | Frank | f#q.com
6 | Jim | f#q.com
My current query produces not one number, but multiple rows, one per email address regardless of how many duplicates of this email are in the table:
SELECT value, count(lds1.leadid) FROM leads_form_element lds1 LEFT JOIN leads lds2 ON lds1.leadID = lds2.leadID
WHERE lds2.typesID = "31" AND lds1.formElementID = '97'
GROUP BY lds1.value HAVING ( COUNT(lds1.value) > 1 )
It's not one query so I'm not sure if it would work in your case, but you could do one query to select the total number of rows, a second query to select distinct email addresses, and subtract the two. This would give you the total number of duplicates...
select count(*) from someTable;
select count(distinct Email) from someTable;
In fact, I don't know if this will work, but you could try doing it all in one query:
select (count(*)-(count(distinct Email))) from someTable
Like I said, untested, but let me know if it works for you.
Try doing a group by in a sub query and then summing up. Something like:
select sum(tot)
from
(
select email, count(1)-1 as tot
from table
group by email
having count(1) > 1
)
For simplicity, I will give a quick example of what i am trying to achieve:
Table 1 - Members
ID | Name
--------------------
1 | John
2 | Mike
3 | Sam
Table 1 - Member_Selections
ID | planID
--------------------
1 | 1
1 | 2
1 | 1
2 | 2
2 | 3
3 | 2
3 | 1
Table 3 - Selection_Details
planID | Cost
--------------------
1 | 5
2 | 10
3 | 12
When i run my query, I want to return the sum of the all member selections grouped by member. The issue I face however (e.g. table 2 data) is that some members may have duplicate information within the system by mistake. While we do our best to filter this data up front, sometimes it slips through the cracks so when I make the necessary calls to the system to pull information, I also want to filter this data.
the results SHOULD show:
Results Table
ID | Name | Total_Cost
-----------------------------
1 | John | 15
2 | Mike | 22
3 | Sam | 15
but instead have John as $20 because he has plan ID #1 inserted twice by mistake.
My query is currently:
SELECT
sq.ID, sq.name, SUM(sq.premium) AS total_cost
FROM
(
SELECT
m.id, m.name, g.premium
FROM members m
INNER JOIN member_selections s USING(ID)
INNER JOIN selection_details g USING(planid)
) sq group by sq.agent
Adding DISTINCT s.planID filters the results incorrectly as it will only show a single PlanID 1 sold (even though members 1 and 3 bought it).
Any help is appreciated.
EDIT
There is also another table I forgot to mention which is the agent table (the agent who sold the plans to members).
the final group by statement groups ALL items sold by the agent ID (which turns the final results into a single row).
Perhaps the simplest solution is to put a unique composite key on the member_selections table:
alter table member_selections add unique key ms_key (ID, planID);
which would prevent any records from being added where the unique combo of ID/planID already exist elsewhere in the table. That'd allow only a single (1,1)
comment followup:
just saw your comment about the 'alter ignore...'. That's work fine, but you'd still be left with the bad duplicates in the table. I'd suggest doing the unique key, then manually cleaning up the table. The query I put in the comments should find all the duplicates for you, which you can then weed out by hand. once the table's clean, there'll be no need for the duplicate-handling version of the query.
Use UNIQUE keys to prevent accidental duplicate entries. This will eliminate the problem at the source, instead of when it starts to show symptoms. It also makes later queries easier, because you can count on having a consistent database.
What about:
SELECT
sq.ID, sq.name, SUM(sq.premium) AS total_cost
FROM
(
SELECT
m.id, m.name, g.premium
FROM members m
INNER JOIN
(select distinct ID, PlanID from member_selections) s
USING(ID)
INNER JOIN selection_details g USING(planid)
) sq group by sq.agent
By the way, is there a reason you don't have a primary key on member_selections that will prevent these duplicates from happening in the first place?
You can add a group by clause into the inner query, which groups by all three columns, basically returning only unique rows. (I also changed 'premium' to 'cost' to match your example tables, and dropped the agent part)
SELECT
sq.ID,
sq.name,
SUM(sq.Cost) AS total_cost
FROM
(
SELECT
m.id,
m.name,
g.Cost
FROM
members m
INNER JOIN member_selections s USING(ID)
INNER JOIN selection_details g USING(planid)
GROUP BY
m.ID,
m.NAME,
g.Cost
) sq
group by
sq.ID,
sq.NAME