Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In the following tables in mysql of users following/being subscribed to other users (related in th 'follow' table) I would like to get the posts posted by the users to which a given user is subscribed:
table 1: follow
|-----------------------------------------|
| id | uid | friends |
|-----------------------------------------|
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 2 | 1 |
|-----------------------------------------|
table 2: posts
|-----------------------------------------------------|
| id | uid | posts | date |
|-----------------------------------------------------|
| 1 | 1 | hai.. | 2013-07-08 01:56:09 |
| 2 | 5 | awesome | 2013-07-08 11:45:50 |
| 3 | 2 | greate!! | 2013-07-09 21:13:29 |
| 4 | 3 | himm.. | 2013-07-10 12:06:10 |
| 5 | 2 | super.. | 2013-07-10 14:50:09 |
|-----------------------------------------------------|
table3: user
|---------------------------|
| uid | name |
|---------------------------|
| 1 | ram |
| 2 | syed |
| 3 | seeta |
|---------------------------|
For example:
Given the user with uid 1, who follows both users with uid 2 and 3, I would like to display my posts and the latest posts of the followes.
The result would look like this:
2 posted
super..
time:2013-07-10 14:50:09
3 posted
himm..
time:2013-07-10 12:06:10
2 posted
greate!!
time:2013-07-09 21:13:29
1 posted
hai..
time:2013-07-08 01:56:09
Suppose your user id is 5:
SELECT * FROM posts WHERE posts.uid IN (SELECT follow.friends FROM follow WHERE follow.uid=5)
Or you can join the tables:
SELECT posts.* FROM posts JOIN follow ON posts.uid = follow.friends WHERE follow.uid=5
If you want to see your own posts as well:
SELECT * FROM posts WHERE posts.uid=5 OR posts.uid IN (SELECT follow.friends FROM follow WHERE follow.uid=5)
I'm not really sure if your tables are in sql, but in case they are, I think the best way to get the data as you want is with a LEFT JOIN as follows:
SELECT * FROM follow
JOIN posts
ON (follow.friends = posts.uid OR follow.uid = posts.uid)
WHERE follow.uid = {user_uid}
Where {user_id} is the user id you want.
If this is the case, please take a further look to joins (f.e. at tizag) and left joins.
Related
I am little confusing for building schema of quiz
In this I have to upload many questions and having four options each option contains textbox and corresponding checkbox that denotes for right answer. if admin select one checkbox that could be right answer.
Note:- In some cases I have uploaded many option 6 to 7 and answers might be 2 or 3 are correct and admin will click on many checkboxes
Can anyone helping in schema
This seems fairly straight forward. You just have three tables, one for quizzes, one for questions and one for answers. Something like this:
Quizzes
+----+-------------+-----------------+
| id | name | description |
+----+-------------+-----------------+
| 1 | Sample Quiz | An example quiz |
+----+-------------+-----------------+
Questions
+----+---------+------------+
| id | quiz_id | question |
+----+---------+------------+
| 1 | 1 | Question 1 |
+----+---------+------------+
Answers
+----+-------------+----------+------------+
| id | question_id | answer | is_correct |
+----+-------------+----------+------------+
| 1 | 1 | Answer 1 | 0 |
| 2 | 1 | Answer 2 | 1 |
| 3 | 1 | Answer 3 | 0 |
| 4 | 1 | Answer 4 | 0 |
+----+-------------+----------+------------+
This schema will support as many quizzes as you need, each with any number of questions and each question can have any number of answers.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i have a task to complete. there is a many to many relationship. the bridge table has been made which looks like
left id right id
+----------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 8 |
| 3 | 1 |
| 3 | 2 |
| 3 | 4 |
| 4 | 1 |
| 4 | 2 |
| 4 | 3 |
| 4 | 5 |
| 5 | 1 |
| 5 | 2 |
| 5 | 4 |
| 5 | 6 |
| 5 | 7 |
+----------+---------+
i have to display the left id = right id in one row
for example
for left id 1
left1 | right1 righ 2
for left id 3
left3 | right1 right2 right 4
how do i do this ? i have tried joining table , doesn't work
I think you can use a simple query to acheive this using GROUP BY and GROUP_CONCAT()
SELECT left_id, GROUP_CONCAT(right_id SEPARATOR ' ') as rigth_id
FROM left-right
GROUP BY left_id;
This is a reasonably straightforward application of GROUP_CONCAT() and GROUP BY. (http://sqlfiddle.com/#!9/ed7e1/2/0)
SELECT leftId,
GROUP_CONCAT(rightId ORDER BY rightId) rightIds
FROM bridge
GROUP BY leftId
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to display up to 3 posts form each category. Also I want to check each category have at least 3 posts (get the post count). Please see the table stretcher below
Category table
+---------+---------------+
| cat_id | cat_name |
+---------+---------------+
| 1 | cat name 1 |
| 2 | cat name 2 |
| 3 | cat name 3 |
+---------+---------------+
Posts table
+------+--------+-------+
| p_id | post | c_id |
+------+--------+-------+
| 1 | post 1 | 1 |
| 2 | post 2 | 1 |
| 3 | post 3 | 2 |
| 4 | post 1 | 2 |
| 5 | post 2 | 1 |
| 6 | post 3 | 3 |
| 6 | post 3 | 1 |
+------+--------+-------+
Query
if($results=$mysqli->query( SELECT * FROM categories LEFT JOIN posts ON posts.p_id= categories.cat_id WHERE posts.p_id= categories.cat_id ORDER BY cat_id LIMIT 0, 10")){
while($row = mysqli_fecth_array($results)){
//Do stuff
}
$results ->close();
}
Any example or comments are appreciated.
It is not possible to get up to 3 posts from each category in MySQL and sane query. You need support for window functions for that. I would preform a separate query for each category. You can read short info in this tag: https://stackoverflow.com/tags/window-functions/info
For the second part:
SELECT c_id, COUNT(*) FROM posts GROUP BY c_id;
select distinct (c.postid) , c.postname, s.category_name from posts c inner join category s on s.cat_id = c.cat_id group by c.cat_id having COUNT(c.cat_id) > 3 limit 3
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have been working with MySQL for a while now, and just recently found the need to manage my data better (MOAR DATA!)...
The problem I am having is this:
Table1: users
- id
Table2: companies
- companyid
- companyname
Table3: customers
- customerid
- companyid
I am trying to query the following.
I have the users ID, I need to use that to get the companyid from customers using the customerid, and return companyname based of the assigned companyid in customers.
It is very possible I am going about this very wrong. I understand that eventually the data is going to get very hard to read by eye as the data starts to grow. My concern is having the ability to associate and disassociate customers from businesses.
If you have any tips, or have a better strategy, or think I should just add this information into the users tables please let me know.
First off, you need some understandings on what Normalization is: http://support.microsoft.com/kb/283878
The database tables are not meant to be "read by eye". I'm pretty sure you are dealing with a very small database now, but imagine in the future you're dealing with thousands of tables with millions of rows, "visual inspection" is not going to work anymore.
A simple join would have given what you need:
SELECT t2.companyname
FROM table1 t1, table2 t2, table3 t3
WHERE t1.id = t3.customerid
AND t3.companyid = t2.company id
AND t3.customerid = (some id) //Depends on what your purpose is,
//this line can also be replaced by
//AND t1.id = (some id)
In your case, it is possible to combine User table and Customer table into one ONLY if all users are customers too. But it is definitely a NO to have company information in either User or Customer tables.
Assuming customers.customerid and users.id will be the same value, this should suffice:
SELECT companies.companyname
FROM customers
LEFT JOIN companies ON customers.companyid = companies.companyid
WHERE customers.customerid = 5
Here is a fiddle
Schema is on the left, sql is on the right.
Tables:
users
+--------+
| ID |
+--------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
+--------+
companies
+------------------------------------------+
| COMPANYID COMPANYNAME |
+------------------------------------------+
| 5 CompAlumpany |
| 9 Dergy Hergins LLC |
| 3 Smergy Berg Inc. |
| 23 Hergin Derz |
| 7 Comperation corpany |
| 11 Contagion Engine |
| 31 AEther Vial |
| 66 Necropotence |
| 90 Lord of Atlantis |
| 65 Snoogins |
| 51 Nickty-Schnickty-Schnoine |
| 58 Take a knee |
| 59 Coorprate |
+------------------------------------------+
customers
+--------------------------+
| CUSTOMERID COMPANYID |
+--------------------------+
| 1 5 |
| 2 9 |
| 3 3 |
| 4 23 |
| 5 7 |
| 6 11 |
| 7 31 |
| 8 66 |
| 9 90 |
| 10 65 |
| 11 51 |
| 12 58 |
| 13 59 |
+--------------------------+
Query Returns:
+---------------------+
| COMPANYNAME |
+---------------------+
| Comperation corpany |
+---------------------+
Posted on behalf of the OP.
I got the results I was looking for with the following:
SELECT t2.companyname FROM companies t2,customers t3 WHERE t3.companyid = t2.companyid AND t3.customerid=?
I was unaware I could create references, this will become very useful for me. Thank you!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
let say we've two tables as following
Table Users
+----+----------+-------+
| id | username | email |
+----+----------+-------+
| 1 | user1 | mail1 |
| 2 | user2 | mail2 |
| 3 | user3 | mail3 |
| 4 | user4 | mail4 |
+----+----------+-------+
Table sales
+----+----------+-------+
| id | username | email |
+----+----------+-------+
| 1 | user1 | mail1 |
| 2 | user3 | mail3 |
+----+----------+-------+
I wanna print the emails of users where their username not found at sales table as if I wanna say select email from users where username not found in table sales
Output should be like this
email2
email4
Please note that i'm usring mysql ~ thanks
There are many possible solutions on this problem. One is by using LEFT JOIN. Any record that has no matching record on the right hand side (Sales) table will have a value of null on its columns which you can filter with.
SELECT a.email
FROM Users a
LEFT JOIN Sales b
ON a.username = b.username
WHERE b.username IS NULL
SQLFiddle Demo
select users.* from users left join sales on sales.username = users.username
where sales.username is null