Get two different limits from mysql left join [closed] - php

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

Related

Get data from more then 2 table against 1 id in php sql [closed]

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 7 months ago.
Improve this question
I have four different table. And invoice_id is foreign key in other table.
i want to show all records against invoice_id.
invoice_id | Bill_amount | Commission_amount | Payment_amount |
| | | |
2 | ------ | 1000 | 500 |
2 | ------ | 200 | 100 |
2 | ------ | -------------- | 100 |
2 | ------ | ------------ | 50 |
3 | 100 | 200 | -------- |
And So On........
Suppose you need 4 columns from 4 different tables. You can use left join if you want to include the empty records also from table2, table3 and table4.
Try the following query:
SELECT
table1.invoice_id,
table2.Bill_amount,
table3.Commission_amount,
table4.Payment_amount
FROM
table1
LEFT JOIN table2 ON table1.invoice_id = table2.invoice_id
LEFT JOIN table3 ON table1.invoice_id = table3.invoice_id
LEFT JOIN table4 ON table1.invoice_id = table4.invoice_id
WHERE
table1.invoice_id = '(your_required_id)';

mysql - many to many query from bridge table [closed]

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

Complex SQL from 4 tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Please I need some help in writing an SQL statement for a PHP application that involves four tables. Here is the situation.
Each Art in Table B has a list of categories in Table D
Each PlaceID in Table A has a list of categories in Table C
I need to select Art from Table B to fill a PlaceID in Table A, but both Art and PlaceID must have at least one category in common.
I will appreciate any help that I can get.
TABLE A
PlaceID |
1 |
2 |
TABLE B
ArtID | Art
1 | Art1
2 | Art2
3 | Art3
TABLE C
ID | PlaceID | Category
1 | 1 | Cat1
2 | 1 | Cat2
3 | 2 | Cat3
4 | 2 | Cat1
5 | 3 | Cat1
TABLE D
TabID | ArtID | Category
1 | 1 | Cat1
2 | 1 | Cat2
3 | 1 | Cat3
4 | 2 | Cat1
5 | 2 | Cat2
For a given myPlaceID you can find possible Art values as follows:
SELECT DISTINCT B.Art
FROM B
INNER JOIN D
ON D.ArtID = B.ArtID
INNER JOIN C
ON C.Category = D.Category
WHERE C.PlaceID = :myPlaceID;
You don't need table A in this query.
Here is a fiddle. Note that for the existing values for PlaceID (1, 2, 3) you will always get 2 records as result (Art1, Art2) with the example data you provided. So you might want to use some other data to get more variation in your results.
Assuming the two tables tablec and tabled are related with Category, then you can do this:
SELECT
b.Art,
a.PlaceId,
COUNT(DISTINCT c.Category) AS TotalCategories
FROM tableA AS a
INNER JOIN tablec AS c ON a.placeID = c.PlaceID
INNER JOIN tabled AS d ON d.Category = c.Category
INNER JOIN tableB AS b ON b.ArtID = d.ArtID
GROUP BY b.Art, a.PlaceId
HAVING COUNT(DISTINCT c.Category) > 0;
This will give you only the places and arts that has at least one category in common, with count of categories in common.
SQL Fiddle Demo
This will give you:
| Art | PlaceID | TotalCategories |
|------|---------|-----------------|
| Art1 | 1 | 2 |
| Art1 | 2 | 2 |
| Art2 | 1 | 2 |
| Art2 | 2 | 1 |
As you can see this returned only art 1, 2 with places 1, 2 as they are the only arts and places have more than one category in common.
Side note: In your table designs, you are missing the many to many junction table between arts and places. So, iF the category is just to relate tablec with tabled, then you can get rid of either tablec or tabled, So that you will have both placeid and artid in the same table that will act as many to many junction table.

How do I sort data from a mysql db according to a unique and predetermined order, NOT asc or desc

I need to show 1000 test questions to a student, 10 per page.
The questions are in a mysql table, the answers will go in another table.
I need each students questions to appear in a different predetermined order than any other students. The sort order is predetermined when they register and placed in a usermeta table.
In the usermeta table there is a column that lists the order in which the questions should be shown. The order in that column is unique to each student and looks like this example: 8|14|97|54|21|37|54|61 ...etc.
The first question to be shown to the student would be question #8, and then question #14, and then question #97, and so on, listing 10 per page.
I don't need to sort the questions asc or desc. Also, I can change the db structure if that would help find a solution.
Also, I can change the db structure if that would help find a
solution.
If changing the db structure is possible, then instead of storing the sorting order as a pipe separated string, store it in a separate table that maps each question to the order it should appear in for a given student. i.e.
student_id, sort_order, question_id
1 1 8
1 2 2
1 3 97
Then join on your sorting table when selecting your questions for a particular student.
SELECT q.* FROM
questions q
JOIN questions_sorting_order qso
ON q.id = qso.question_id
ORDER BY qso.sort_order
WHERE qso.student_id = :student_id
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
SELECT i2.i*10+i1.i x
, FIND_IN_SET(i2.i*10+i1.i,'8,14,97,54,21,37,54,61') y
FROM ints i1
, ints i2
HAVING y > 0
ORDER
BY y;
+----+---+
| x | y |
+----+---+
| 8 | 1 |
| 14 | 2 |
| 97 | 3 |
| 54 | 4 |
| 21 | 5 |
| 37 | 6 |
| 61 | 8 |
+----+---+
Note that 54 is ignored second time around

get the follow posts based on date [closed]

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.

Categories