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
Related
I have two tables and I want to join them to get the desired output.
Say the 1st table (seat1) is
and the 2nd table (collegestudents) is
The desired output is
I have tried the below code. But it fails to give the desired result.
$rde2=mysqli_query($con, "select * from seat1 s
left JOIN collegestudents c ON c.Roll = s.Roll
");
Any help please.
You want a left join. Your query looks fine, but you would need not to use select *, and instead explictly list the columns that you want to select, using table prefixes. Otherwise, since you have a Roll column in both tables, a name clashes will happen, that your application apparently does not handle well.
select
s.Roll,
c.Name,
s.Subject
from seat1 s
left join collegestudents c on c.Roll = s.Roll
I have 2 MySQL tables:
I store the admin_vat_id in the Fac__Article table which is actually a reference to the id of the Fac__Admin_vat:
What I'm trying to do
I want to get all Fac__Article table's entries, but at the admin_vat_id column, where it normally would display the integer value, I want to display the float value of the column rate of the table Fac__Admin_vat.
I guess I have to use the select and union keyword, but I don't know how to implement this select query. Please guide me with knowledge in solving this problem.
All you need is a simple LEFT JOIN:
SELECT
fa.id,
fav.rate,
fa.article_number,
fa.name,
fa.description,
fa.unit,
fa.price,
fa.stock,
fa.stock_warning,
fa.visible
FROM `fac_article` fa
LEFT JOIN `fac_admin_vat` fav
ON fa.admin_vat_id = fav.id
SQL Fiddle
Try this (correct the table names if i misspelled): SELECT *, Fac__Admin_vat.rate FROM Fac__Article LEFT JOIN Fac__Admin_vat ON Fac__Admin_vat.id = Fac__Article.admin_vat_id
Try using left join, a rough example below.
SELECT *, Fac__Admin_vat.rate as admin_rate FROM Fac__Article LEFT JOIN Fac__Admin_vat ON Fac__Article.admin_vat_id = Fac__Admin_vat.id
This will allow you to use admin_rate in your code to get the data you want. Hope this helps.
I'm trying to write a simple interface for a list of companies using MySQL and PHP. So, I want to fetch some information from my database.
Here are my tables:
companies_data - only for system information.
corporate_data - here I want to keep information about big companies.
individual_data - and here I want to keep information about little companies.
So, here is the tables
And here is the query that I've written:
SELECT
a.id,
a.user_id,
a.added,
a.`status`,
a.company_id,
a.company_type,
a.deposit,
a.individual_operations_cache,
a.corporate_operations_cache,
a.physical_operations_cache,
b.full_name,
b.tax_number,
b.address,
b.statement_date,
b.psrn,
c.full_name,
c.tax_number,
c.address,
c.statement_date,
c.psrn
FROM
companies_data a
LEFT OUTER JOIN corporate_data b
ON (a.company_id = b.id) AND a.company_type = 0
LEFT OUTER JOIN individual_data c
ON (a.company_id = c.id) AND a.company_type = 1
WHERE
a.user_id = 3
This is just the code for a test, I'll expand it soon.
As you see, I've got result with extra fields like %field_name%1, %another_field_name%1 and so on. Of course it is not the mysql error - what I've asked that I've got - but I want to remove this fields? It's possible or I must convert this output on the application side?
thos %field_name%1, %another_field_name%1 , are visible since you are selecting them in your query:
b.full_name,
b.tax_number,
b.address,
b.statement_date,
b.psrn,
c.full_name,
c.tax_number,
c.address,
c.statement_date,
c.psrn
When you use fields with the same name in distinct tables, then the result column name come with this identifier field1, field2, fieldn... in order to distinguish from which table does the field come from.
If you want to avoid this names, you can use aliases as follows:
[...]
b.full_name as corporate_full_name,
[...]
Probably, if every common fields are coincident, you won´t need to show them all, so just remove them from the select.
Hope being usefull for you.
Br.
My database has a challenges table where there are these columns: Challenge_Name, Challenge_Description. I have a 2nd table called completed_challenges_junction and it has these columns: Member_Name, Challenge_Name.
I need a way to display all of the challenge names from the challenges table along with the member names within the completed_challenges_junction. If there is no match then I would like it to display NULL.
I think I'm pretty close to having my SQL code working, here is what I have now.
SELECT challenges.Challenge_Name, challenges.Challenge_Description, completed_challenges_junction.Member_Names
FROM challenges
LEFT JOIN completed_challenges_junction ON challenges.Challenge_Name=completed_challenges_junction.Challenge_Name
This works but also bring duplicate entries of another member. If i use WHERE Member_Name='testmember' it only brings the entries of the member when I need it to still display all Challenge_Names.
SELECT
A.Challenge_Name,A.Challenge_Description,
GROUP_CONCAT(IFNULL(B.Member_Names,'')) Member_Names
FROM
challenges A
LEFT JOIN completed_challenges_junction B ON
A.Challenge_Name=B.Challenge_Name AND
A.Challenge_Description=B.Challenge_Description
GROUP BY
A.Challenge_Name,A.Challenge_Description
;
or
SELECT
A.Challenge_Name,A.Challenge_Description,
GROUP_CONCAT(IFNULL(B.Member_Names,'')) Member_Names
FROM
challenges A
LEFT JOIN completed_challenges_junction B
USING (Challenge_Name,Challenge_Description)
GROUP BY
A.Challenge_Name,A.Challenge_Description
;
You could add the WHERE clause into the ON condition like this
SELECT challenges.Challenge_Name, challenges.Challenge_Description, completed_challenges_junction.Member_Names
FROM challenges
LEFT JOIN completed_challenges_junction ON (
challenges.Challenge_Name=completed_challenges_junction.Challenge_Name
AND Member_Name='testmember'
)
This way only testmember entries will come up from completed_challanges_junction, but all challenges will be displayed.
I'm not sure what exactly you want, but maybe you are looking for 'group by' clause and group_concat function:
SELECT challenges.Challenge_Name, challenges.Challenge_Description,
GROUP_CONCAT(completed_challenges_junction.Member_Names SEPARATOR ', ')
FROM challenges
LEFT JOIN completed_challenges_junction
ON challenges.Challenge_Name=completed_challenges_junction.Challenge_Name
GROUP BY challenges.Challenge_Name
This query will return result where each Challenge_Name occurs only once.
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.