SQL join produces one result only - php

Can anyone please tell me why this result is generation only one results? taking in mind that everything is set right and the three tables are populated correctly, i took out the group_concat and it worked but of course with a php undefined index error!
SELECT
`songs`.`song_name`,
`songs`.`add_date`,
`songs`.`song_id`,
`songs`.`song_picture`,
group_concat(DISTINCT artists.artist_name) as artist_name
FROM (`songs`)
JOIN `mtm_songs_artists` ON `songs`.`song_id` = `mtm_songs_artists`.`song_id`
JOIN `artists` ON `artists`.`artist_id` = `mtm_songs_artists`.`artist_id`
ORDER BY `songs`.`song_id` DESC
LIMIT 10
so i'm guessing it's something related to group_concat.
best regards,
Rami

You're right, it's because of the group_concat. group_concat concatenates all items in the group... which is the entire result set if you have no GROUP BY clause. It's a bit hard to tell how to fix the query without the schema, but I think you need to either group by artist_id or use a subquery.

It should be concat not group_concat in my case.

Related

How to get only 1 row of each similar content?

I Have a private message system on my site and i'm trying to pull all subject's on the messages.
I need to show any subject once.
so if I have this subjects:
hey
hello
hey
good morning
good morning
I need to print this:
hey
hello
good morning
I can just cross it out with if, but I guess there is a better way with sql.
Thank you.
Option 1 : DISTINCT
SELECT DISTINCT subject
FROM my_table
Option 2 : GROUP BY
SELECT subjects
FROM my_table
GROUP BY subjects
Difference between GROUP BY and DISTINCT
Distinct is used to filter unique records out of the records that satisfy the query criteria.
Group by clause is used to group the data upon which the aggregate functions are fired and the output is returned based on the columns in the group by clause. It has its own limitations such as all the columns that are in the select query apart from the aggregate functions have to be the part of the Group by clause.
See this and this for the reference .
Try something like:
SELECT DISTINCT subject FROM emails;
If you use SQL.
select distinct subjects
from YOUR_TABLE.
or
Select subjects
from YOU_TABLE
group by subjects

Order a table by a column in another table

I have looked at other questions/answer regarding my question, but for some reason, every time I try to implement an answer exactly how they implemented it, it throws an error. But I am paginating some things now, and I need to order my results by column in another table.
Here is the code I have now:
SELECT `id`,`name`,`players`,`max_players`,`status`,`host`,`port`
FROM `servers`
LIMIT :to,:from
The table it is getting stuff from is the servers table, and it needs to get the column rank from the server_profiles table, and order it by that. I try to understand how MySQL joins work, but they always seem to confuse me, from looking at examples, to reading the markup on the MySQL wiki.
You can use join for that. For that you need to have a relation between both tables. I have used server.ID = server_profiles.serversID for example.
SELECT s.* FROM servers s
JOIN server_profiles sp
ON s.ID = sp.serversID
ORDER BY sp.rank
LIMIT :to,:from
See this SQLFiddle

MySql Query help required in getting result as per desire

Can someone please help me in writing the exact query, which can give me desired results for the following,
I wrote,
SELECT tbl_order_detail.order_id,
tbl_order_detail.order_title,
tbl_order_detail.dealer_id
FROM tbl_order_detail
LEFT JOIN tbl_order_lead_send_detail ON tbl_order_detail.order_id=tbl_order_lead_send_detail.order_id
WHERE tbl_order_detail.order_status = 'Active'
and finding the dealer name from one php function that takes dealer_id in it and returns dealer_name (using another mysql query), and count from another mysql query
but it isn't giving my desired output. it's giving output as
But I want the above circled ones in one row only, as everything is same in them, but they are showing many times in the output (why not one).
Can someone help me in this?
If you GROUP BY order_id, you should be good to go
Edit:
To get the dealer name, just JOIN the dealer table and select the dealer name column.
Untested, may contain errors
SELECT
tbl_order_detail.order_id,
tbl_order_detail.order_title,
dealer.dealer_name,
COUNT(tbl_order_lead_send_detail.order_id) AS total_customers_count
FROM tbl_order_detail
JOIN dealers ON (tbl_order_detail.dealer_id = dealers.dealer_id)
LEFT JOIN tbl_order_lead_send_detail ON (tbl_order_detail.order_id = tbl_order_lead_send_detail.order_id)
WHERE tbl_order_detail.order_status = 'Active'
GROUP BY tbl_order_detail.order_id, tbl_order_detail.order_title, dealer.dealer_name;
To get a count, use COUNT(). To group your results by Order ID, use GROUP BY tbl_order_detail.order_id - one of the upsides of MySQL (or SQL in general) is that it's fairly close to plain English.
Try this:
SELECT OD.order_id,
OD.order_title,
OD.dealer_id,
SD.customer_id,
COUNT(SD.id) AS NumCustomers
FROM tbl_order_detail OD
LEFT JOIN tbl_order_lead_send_detail SD
ON OD.order_id=SD.order_id
WHERE OD.order_status='Active'
GROUP BY OD.order_id,
OD.order_title,
OD.dealer_id,
SD.customer_id

Getting distinct values in this query

Hi, this is my query:
SELECT tbl_order_detail.order_id, tbl_order_lead_send_detail.customer_id, tbl_order_detail.order_title, tbl_order_detail.dealer_id , tbl_order_lead_send_detail.send_date_time
FROM tbl_order_detail
INNER JOIN tbl_order_lead_send_detail
ON tbl_order_detail.order_id=tbl_order_lead_send_detail.order_id
where tbl_order_detail.order_status='Active'
ORDER BY tbl_order_lead_send_detail.send_date_time DESC
I am getting this output,
I want to get only one data-row for one means distinct value of Order ID. How can I change my sql query to get my desired result?
SELECT distinct(tbl_order_detail.order_id), tbl_order_lead_send_detail.customer_id, tbl_order_detail.order_title, tbl_order_detail.dealer_id , tbl_order_lead_send_detail.send_date_time
FROM tbl_order_detail
INNER JOIN tbl_order_lead_send_detail
ON tbl_order_detail.order_id=tbl_order_lead_send_detail.order_id
where tbl_order_detail.order_status='Active'
ORDER BY tbl_order_lead_send_detail.send_date_time DESC
SELECT DISTINCT tbl_order_detail.order_id, ...
Two possibilities:
1) Select distinct ..
2) Select ... group by tbl_order_detail.order_id, tbl_order_lead_send_detail.customer_id, tbl_order_detail.order_title, tbl_order_detail.dealer_id , tbl_order_lead_send_detail.send_date_time
Be aware that even if you use the keyword distinct in your query, it'll still return more than one rows for the same order id if at least one of the columns return a different data.
Your result image shows 4 columns, while your query asked for 5; so, it's not 100% possible to determine where the problem lies.
That being said, use select distinct, and see if it solves your problem. If it doesn't, you might have to remove the column(s) with the different data from the query.
Happy coding!
From some of your screenshots it appears that customer id is different for each row. You need to show all the output to get sensible answers.
I'm pretty certain SELECT DISTINCT isn't broken, so you must be selecting non-unique rows.

MySQL LEFT JOIN, INNER JOIN etc, complicated query, PHP + MySQL for a forum

So I've got a little forum I'm trying to get data for, there are 4 tables, forum, forum_posts, forum_threads and users. What i'm trying to do is to get the latest post for each forum and giving the user a sneak peek of that post, i want to get the number of posts and number of threads in each forum aswell. Also, i want to do this in one query. So here's what i came up with:
SELECT lfx_forum_posts.*, lfx_forum.*, COUNT(lfx_forum_posts.pid) as posts_count,
lfx_users.username,
lfx_users.uid,
lfx_forum_threads.tid, lfx_forum_threads.parent_forum as t_parent,
lfx_forum_threads.text as t_text, COUNT(lfx_forum_threads.tid) as thread_count
FROM
lfx_forum
LEFT JOIN
(lfx_forum_threads
INNER JOIN
(lfx_forum_posts
INNER JOIN lfx_users
ON lfx_users.uid = lfx_forum_posts.author)
ON lfx_forum_threads.tid = lfx_forum_posts.parent_thread AND lfx_forum_posts.pid =
(SELECT MAX(lfx_forum_posts.pid)
FROM lfx_forum_posts
WHERE lfx_forum_posts.parent_forum = lfx_forum.fid
GROUP BY lfx_forum_posts.parent_forum)
)
ON lfx_forum.fid = lfx_forum_posts.parent_forum
GROUP BY lfx_forum.fid
ORDER BY lfx_forum.fid ASC
This get the latest post in each forum and gives me a sneakpeek of it, the problem is that
lfx_forum_posts.pid =
(SELECT MAX(lfx_forum_posts.pid)
FROM lfx_forum_posts
WHERE lfx_forum_posts.parent_forum = lfx_forum.fid
GROUP BY lfx_forum_posts.parent_forum)
Makes my COUNT(lfx_forum_posts.pid) go to one (aswell as the COUNT(lfx_forum_threads.tid) which isn't how i would like it to work. My question is: is there some somewhat easy way to make it show the correct number and at the same time fetch the correct post info (the latest one that is)?
If something is unclear please tell and i'll try to explain my issue further, it's my first time posting something here.
Hard to get an overview of the structure of your tables with only one big query like that.
Have you considered making a view to make it easier and faster to run the query?
Why do you have to keep it in one query? Personally I find that you can often gain both performance and code-readability by splitting overly complicated queries into more parts.
But hard to get an overview so can't really give a good answer to your question:)
Just add num_posts column to your table. Don't count posts with COUNT().
Can we get some...
Show Tables;
Desc Table lfx_forum_posts;
Desc Table lfx_forum_threads;
Desc Table lfx_forum_users;
Desc Table lfx_forum;
Here's some pseudo code
select f.*, (select count(*) from forum_posts fp where fp.forum_id = f.id) as num_posts,
(select count(*) from forum_threads ft where ft.forum_id = f.id) as num_threads,
(select max(fp.id) from forum_posts fp
where fp.id = f.id
) as latest_post_id,
from forums f;
Then go on to use latest_post_id in a seperate query to get it's information.
if it doesn't like using f before it's declared then make a temporary table for this then you update every time the query is ran.

Categories