I have two tables-
1) ****Company_Form****
[Contract_No#,Software_Name,Company_Name,Vendor_Code]
2) ****User_Form****
[Contract_No,Invoice_No#,Invoice_Date,Invoice_Amount,Invoice_Submit_Date]
Fields denoted with # and bold are primary keys.
=>The user has to enter a software name for which he wants to get the data of.
=>I have to structure a query in which I have to display the result in the following form:
[Contract#,Software_Name,Company_Name,Invoice_No,Invoice_Date,Invoice_Submission_Date]
Now,
one Contract_No can contain many Invoice_no under its name in
the User Form table.
One Contract_No can occur one time only in
Company_Form table
The retrieved records have to be group by the latest Invoice_Date
I came to the logic that:
I have to first retrieve all the contract numbers with that software
name from Company_Form table.
I have to query that contract number from User_Form table and display
the data for each matched contract no. fetched from Company_Form
table.
The problem is that I am unable to structure a query in SQL that can do the task for me.
Kindly help me in formulating the query.
[PS] I am using SQL with PHP.
I tried a query like:
I tried one approach as :
SELECT a.ContractNo,a.SoftwareName,a.CompanyName,b.InvoiceNo,b.InvoiceDate,b.InvAmount,b.InvoiceSubmitDate
FROM Company_Form as a,User_Form as b
WHERE b.ContractNo IN(SELECT ContractNo FROM Company_Form WHERE
SoftwareName='$Sname') AND a.ContractNo=b.ContractNo;
But I am getting a error that sub query returns more than 1 row.
Can I get help from this?
I am assuming you are attempting to find the most recent price of the user selected software and its corresponding invoice. Here is an approach to do this. If this is tested to your satisfaction, I can add necessary explanation.
select uf.Contract_No#,
cf.Software_Name,
cf.Company_Name,
uf.Invoice_No#,
uf.Invoice_Date,
uf.Invoice_Amount,
uf.Invoice_Submit_Date
from User_Form uf
inner join (
-- Most recent sale of software
select Contract_No#, max(Invoice_Date)
from User_Form
group by Contract_No#
) latest
on (
-- Filter via join for latest match records
uf.Contract_No# = latest.Contract_No#
and uf.Invoice_Date = latest.Invoice_Date
)
inner join Company_Form cf
on cf.Contract_No# = uf.Contract_No#
where cf.Software_name = :software_name
If the requirement allows your sub query to return more than one row, I would suggest you to use IN instead of = in the where clause of your main query.
Please note that I have just looked at the query and have not fully understood the requirements.
Thanks.
I worked around for some time and finally came to the following query which works like a charm
SELECT a.ContractNo,a.SoftwareName,a.CompanyName,b.InvoiceNo,b.InvoiceDate,b.InvAmount,b.ISD
FROM Company_Form as a,User_Form as b
WHERE b.ContractNo IN (SELECT ContractNo FROM Company_Form WHERE SoftwareName='$Sname')
AND a.ContractNo=b.ContractNo;
If anybody needs help in understanding the logic of this query,feel free to comment below.
Related
I’m using Ajax to retrieve information from my database. I have no trouble sending to server PHP and getting information back using simple SQL queries. I came across a section that I needed to pull from 2 tables (Same Database) Both in common are column PO. Attached is a picture of an example.
I have been trying to pull everything from one table that meets my condition like Month of, year of and Store. I have been able to JOIN them but not successfully. Closest I ever got is it checks both tables and only returns the data that have matching PO and not the rest of Table 1.
I like for it to retrieve all rows in table one that meet the conditions and if there is a matching PO in table2 join it else continue to retrieve from table1.
Please any help would be grateful
I figured it out. Thank you
$sql = "SELECT * FROM weekly_report LEFT JOIN tracker ON weekly_report.PO = tracker.POt WHERE MONTH(STR_TO_DATE(`Need By Date (Date)`, '%m-%d-%Y')) = '$month' AND YEAR(STR_TO_DATE(`Need By Date (Date)`, '%m-%d-%Y')) = '$year' AND weekly_report.`Ship To Location (Location ID)` LIKE '%$store%' GROUP BY (weekly_report.PO)";
This is my detail table:
This is my contact table:
Here are two rows in details table and 4 rows (each details have 2) in contact table. When I use join query I get 4 results row but I want only 2 row(one row of details row with one contact of that corresponding details).
my query:
$this->db->select('*');
$this->db->from('dots_center_detail');
$this->db->join('dots_center_contact', 'dots_center_contact.registration_id = dots_center_detail.registration_id','left');
try this
$this->db->query("
SELECT DISTINCT dots_center_detail.registration_id, dots_center_contact.contact
FROM dots_center_detail
LEFT JOIN dots_center_contact ON dots_center_contact.registration_id = dots_center_detail.registration_id
")
Using select('DISTINCT *') in place of select('*') may give the result you need. It's worth a try.
Or you can create a view like this to go with your table definitions.
CREATE OR REPLACE VIEW dots_center_unique_contact AS
SELECT DISTINCT * FROM dots_center_contact;
Then refer to that dots_center_unique_contact view in your join operation.
Your best bet long term is to figure out why you have duplicate rows, and tighten up your business rules so you don't.
I'm building a sales system for a company, but I'm stuck with the following issue.
Every day I load .XML productfeed into a database called items. The rows in the productfeed are never in the same order, so sometimes the row with Referentie = 380083 is at the very top, and the other day that very same row is at the very bottum.
I also have to get all the instock value, but when I run the following query
SELECT `instock` FROM SomeTable WHERE `id` > 0
I get all values, but not in the same order as in the other table.
So I have to get the instock value of all rows where referentie in table A is the same as it is in table B.
I already have this query:
select * from `16-11-23 wed 09:37` where `referentie` LIKE '4210310AS'
and this query does the right job, but I have like 500 rows in the table.
So I need to find a way to automate the: LIKE '4210310AS' bit, so it selects all 500 values in one go.
Can somebody tell me how that can be done?
I'm not even sure I understand your problem...
Don't take this personally, but you seem to be concerned/confused by the ordering of the data in the tables which suggests to me your understanding of relational databases and SQL is lacking. I suggest you brush up on the basics.
Can't you just use the following query?
SELECT a.referentie
, b.instock
FROM tableA a
, tableB b
WHERE b.referentie = a.referentie
After searching for a damn long time, I've not found a query to make this happen.
I have an "offers" table with a "listing_id" field and a "user_id" field and I need to get ALL the records for all listing_id's where at least one record matches the given user_id.
In other words, I need a query that determines the listing_id's that the given user is involved in, and then returns all the offer records of those listing_id's regardless of user_id.
That last part is the problem. It's getting all the other user's offer records to return when I'm only providing one user's id and no listing id's
I was thinking of first determining the listing_ids in a separate query and then using a php loop to create a WHERE clause for a second query that would consist of a bunch of "listing_id = $var ||" but then I couldn't bring myself to do it because I figured there must be a better way.
Hopefully this is easy and the only reason it has escaped me is because I've had my head up my ass. Will be happy to get this one behind me.
Thanks for taking the time.
Josh
You could do two queries playing along on the MySQL side, like this:
SELECT * FROM offers WHERE listing_id IN (SELECT listing_id FROM offers WHERE user_id = 1)
If I understand what you are after you should join offers on itself on listingid match and userid = given
select * from offers AS t1
inner join offers AS t2 on t1.listingid = t2.listingid and t1.userid = 1;
First of all, I am new here and just learning sql, so bear with me.
I have a simple question. Let's say I have a table called voting with the following columns:
id, token(int), candidate(int), rank(int)
.. and I want to perform a query like:
SELECT *
FROM voting
WHERE rank > t1.rank
AND token = t1.token
.. where t1 is
SELECT rank,token
FROM voting
WHERE candidate = $mycandidate
How can I do this in a single statement using AS alias, or whatever is the simplest method?
Please note that final table created may have rows with different candidates than i have specified i.e the rank,token variables are initially chosen according to candidate but once chosen they may contain any candidate value with them.
Something like this should do, I believe:
select
v1.*
from
voting v1 inner join
voting v2
on v1.token = v2.token and
v2.candidate = $mycandidate and
v1.rank > v2.rank
EDIT changed SELECT * to SELECT v1.*