SQL not Returning Desired Results - php

I am trying to generate result from a SQL, but i am not getting desired results.
above image is the result of following query
SELECT DISTINCT
U.USERID, U.shopName, U.image, P.PID
FROM users U, products P
WHERE P.USERID=U.USERID
GROUP BY U.USERID
ORDER BY P.PID DESC
Now what i want to do is that there is another record for USERID 2 and 3 with PID 3 and 5 respectively, let me show you the image
I want to retrieve latest record entered in the table but with distinct USERID
e-g i want to retrive this
userid | shopname | image | PID
3 | shop name | image | 5
4 | shop name | image | 4
2 | shop name | image | 3
any help will be appreciable.

select * from "table" grou by ID and order by Desc

Use it like below, Do group by with PID.
SELECT DISTINCT U.USERID, U.shopName, U.image, P.PID FROM users U, products P WHERE P.USERID=U.USERID GROUP BY P.PID ORDER BY P.PID DESC

I am not sure if I understood your problem, but perhaps the following query solves your problem:
SELECT U.USERID, U.shopName, U.image, P.PID
FROM users U, products P
WHERE P.USERID=U.USERID
AND P.PID in (select min(p2.pid) from products p2 where u.userid = p2.userid)
A word of warning: it might be more efficient to use the ORDER BY trick and filter the results externally.

Related

Dynamic Query to Select Multiple Entries from a Relational Record Table

Let's say I have a table which stores the relation between products and their categories:
p_id | c_id
-----+-----
1 | 1
1 | 2
2 | 1
2 | 2
2 | 3
3 | 2
As you can see, a product might have multiple categories. How can I search for products that have categories 1 and 2 assigned? The closest I can think of is using JOIN:
SELECT a.p_id
FROM rel_table a
JOIN rel table b
ON a.p_id=b.p_id AND b.c_id=2
WHERE a.c_id=1
While this achieves what I want, it is not practical because my query will be dynamic. If I have to select products with 3 categories, this requires a difficult change in the query.
Is there a cleaner and more clever way to achieve this? I imagine something that selects first set, then refines with another category for the amount of levels needed.
You should use IN or Between for such things. You can dynamically create the values you put in the IN/BETWEEN
SELECT a.p_id
FROM rel_table a
WHERE a.c_id IN (1,2,3)
group by a.p_id
having count(1) = 3
order by a.p_id asc
or
SELECT a.p_id
FROM rel_table a
WHERE a.c_id between 1 and 3
group by a.p_id
having count(1) = 3
order by a.p_id asc

How to get one status update from users

I have a question about MySQL Query. I have a slider on the homepage to show the newest user status updates. I am using JOIN table to do this. Everything work well expect the status update is not recognize with user own status.
There is my table row:
users
id | username | name | last_login | status | date_registered
posts
userid | post_details | date_posted | status | facebook | twitter
There is my query:
"SELECT U.id, U.username, U.name, U.last_login, U.status, P.userid, P.post_details, P.date_posted
FROM users U
LEFT JOIN posts P ON U.id = P.userid
WHERE U.status='active' AND P.status='1'
ORDER BY U.last_login DESC,
LIMIT 12"
Problem when I am using above query's user status update not recognize with ordered user slider. When I add AND P.userid=U.id in WHERE line, only 1 user is displayed.
Please tell me a better way to make status updates is recognized with user.
Preview:

MySql - Get latest rows from table without duplicate subject id

I am attempting to get the latest threads that have been posted on in a Forum with vars from Users, Threads and the last Post. Problem is that the current method i am attempting brings back duplicate threads because the newer posts have been posted in those threads, whereas i just want one post to return per thread, not all the latest posts.
SELECT t.thread_id, u.user_name, p.post_entry
FROM forum_thread as t
LEFT JOIN forum_post AS p ON p.post_thread = t.thread_id
LEFT JOIN user AS u ON u.user_id = p.post_user
ORDER BY t.thread_lastpost DESC LIMIT 0,8
Currently that is returning:
/-----------------------------------------/
| 7049 | USERNAME | Post Entry |
|----------------------------------------|
| 7049 | USERNAME | Post Entry |
|----------------------------------------|
| 7049 | USERNAME | Post Entry |
|----------------------------------------|
| 7049 | USERNAME | Post Entry |
|----------------------------------------|
| 7650 | USERNAME | Post Entry |
|----------------------------------------|
| 7068 | USERNAME | Post Entry |
|----------------------------------------|
| 7056 | USERNAME | Post Entry |
|----------------------------------------|
| 7136 | USERNAME | Post Entry |
I want to remove those first duplicate IDs and only leave one with the latest post entry from that thread.
I hope i have explained it well enough for people to understand.
Thanks.
----------------- EDIT --------------------
Got it to work with GROUP BY:
SELECT t.thread_id, u.user_id, p.post_entry
FROM forum_post AS p
LEFT JOIN forum_thread AS t ON t.thread_id = p.post_thread
LEFT JOIN user AS u ON u.user_id = p.post_user
GROUP BY t.thread_id
ORDER BY t.thread_lastpost DESC LIMIT 0,8
this query may not be best performing, but I can't do much more without knowing structure of your database. You'll need some post_id or post_timestamp for including the second left join as well. Neither DISTINCT nor GROUP BY would solve your issue, as the username and post_entry will usually be different in all cases, i.e. the lines won't actually be distinct.
SELECT t.thread_id, u.user_name, p.post_entry
FROM forum_thread as t
LEFT JOIN forum_post AS p ON p.post_thread = t.thread_id
LEFT JOIN forum_post AS p2 ON p.post_id > p2.post_id
LEFT JOIN user AS u ON u.user_id = p.post_user
where p2.post_id is null
ORDER BY t.thread_lastpost DESC LIMIT 0,8

php&mysql find parent category name

I have problem about inner join, left join commands.
My category table is:
ID | parent | title
1 | 0 | First Category
2 | 1 | Other Category
I have list categorys and I want get parents category title at sql command.
I have tried:
SELECT cat.ID, cat.title, cat2.title as parentcatname, cat.parent
FROM categories cat INNER JOIN categories cat2 ON cat2.ID=cat.parent
But ıt's not working.
You have to use LEFT JOIN to be able to pull all categories no matter have they parent category or not. INNER JOIN filters out all mismatches.
SELECT c.id, c.title, c.parent, p.title parent_title
FROM categories c LEFT JOIN categories p
ON c.parent = p.id
Output:
| ID | TITLE | PARENT | PARENT_TITLE |
-------------------------------------------------
| 1 | First Category | 0 | (null) |
| 2 | Other Category | 1 | First Category |
Here is SQLFiddle demo
If you want to get all parent categories then try query
SELECT cat_id FROM categories WHERE parent=0;
If you want get parent category of a category
SELECT C.cat_id, P.title FROM categories C LEFT JOIN categories P ON P.parent=C.cat_id;
I haven't tested above code but it should work fine.
You can always debug your SQL by entering it into a validator (loads online), phpMyAdmin's SQL tab or a editor with SQL validation. It looks to me like you have a small typo near your categories table selection.
Always dumb it down if your SQL isn't working. Note that JOINs (inner, left, right, ect) are meant to join TWO or MORE tables.
SELECT
one.ID, one.title, one.parent, one.title, one.parent, one.title
FROM
categories one
LEFT JOIN
categories two
ON
one.parent = two.ID

Problem with left outer join

I have 2 tables.
Table 1 : t_atc_list
id | a_name | s_title | r_name
------------------------------
80 | ss | 128 | 5
Where s_title & r_name is foreign key.
Table 2 : t_s_list
s_id | title
-------------
128 | Song Title
I want have used left join query on this..to select all values of 't_atc_list' if it mightbe in 't_s_list'..
$query=mysql_query("SELECT t.s_title, s.title from t_atc_list t LEFT OUTER JOIN t_s_list s ON t.s_title=s.s_id");
$row=mysql_fetch_array($query);
While if I use right join its working..
$query=mysql_query("SELECT t.s_title, s.title from t_s_list s RIGHT OUTER JOIN t_atc_list t ON t.s_title=s.s_id");
$row=mysql_fetch_array($query);
Whats the reason left join is not working but right join is going well? And I think both the queries are identical than whats the problem?
This is a strictly data related issue you have there, your SQL syntax is correct, but your data must not line up.
For a visualization of SQL, look here: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Also if you do a right join, that means you are simply getting data from the 2nd table, and nulls in the first. I am just guessing here as I see no real data examples.

Categories