SELECT SUM(T.KDV) AS TOTALKDV
FROM (SELECT *
FROM pharmana_urun_db.general_Table, pharmana_Hareket_db.general_Table
WHERE pharmana_urun_db.general_Table.Barkod = pharmana_Hareket_db.general_Table.Barkod AND pharmana_Hareket_db.general_Table.EczaneID = '".$pharmacy_id"') AS T
GROUP BY T.Kategori
1060 - Duplicate column name 'Barkod',
how can I avoid this?
(SELECT *
FROM pharmana_urun_db.general_Table, pharmana_Hareket_db.general_Table
This selects two columns with the same name Barkod
You need to use the explicit join syntax as Jens suggested
Nice explanation here
Learn to use proper JOIN syntax. Don't use subqueries unnecessarily.
You should write this query as:
SELECT SUM(T.KDV) AS TOTALKDV
FROM pharmana_urun_db.general_Table gt1 JOIN
pharmana_Hareket_db.general_Table gt2
USING (Barkod)
WHERE gt2.EczaneID = '".$pharmacy_id"'
GROUP BY Kategori;
You should also learn to use parameters to pass values into SQL queries, rather than munging query strings. I also suspect that you should be including Kategori in the SELECT.
Related
SELECT * FROM `continue_paper`JOIN `questions`
WHERE `questions`.q_id IN
`continue_paper`.cp_qu_id AND `continue_paper`.cp_id=1
in your code you need a set of values for the IN clause in your case you have not so you should use =
SELECT *
FROM continue_paper
JOIN questions ON questions.q_id =continue_paper.cp_qu_id
AND continue_paper.cp_id=1
and you could use a ON clause instead of where
It's usually good practice to use JOIN with comparison operators.
I would join the tables using the foreign/primary keys, then filter it with IN if needed.
Specifically, I would approach it this way:
SELECT * FROM `continue_paper` JOIN `questions`
WHERE `questions`.q_id =
`continue_paper`.cp_qu_id AND `continue_paper`.cp_id=1
my first query :
SELECT `oc_banner_image_description`.`title`
FROM `oc_banner_image_description`
WHERE `banner_id`=9
my second query:
SELECT `oc_banner_image`.`image` FROM `oc_banner_image` WHERE `banner_id`=9
how to make this two queries into single query using sql joins.
Using standard join syntax would look like this :
SELECT `oc_banner_image_description`.`title`, `oc_banner_image`.`image`
FROM `oc_banner_image_description`
JOIN `oc_banner_image` ON `oc_banner_image_description`.`banner_id` = `oc_banner_image`.`banner_id`
WHERE `oc_banner_image`.`banner_id`=9
Try this (You may need to use the single quotes a bit different than what I have)
SELECT `i`.`image`, `d`.`title`
FROM `oc_banner_image` as `i`, `oc_banner_image_description` as `d`
WHERE `c.banner_id` = `i.banner_id`
and i.`banner_id`=9
If this is not working try this on both tables
select banner_id, count(banner_id)
from oc_banner_image
group by banner_id order desc;
This will tell you if you have multiple banner_id's in the oc_banner_image table.
Try this
SELECT bannerDesc.title , bannerImage.image
FROM oc_banner_image_description bannerDesc join oc_banner_image bannerImage
on bannerDesc.banner_id = bannerImage.banner_id
WHERE bannerImage.banner_id=9
select * from user_levels
join collectors_users on user_levels.id = collectors_users.user_level
where collectors_users.username = 'testuser'
I want it to pull everything from user_levels and nothing from collectors_users. But it's pulling from both. How do I correct the statement?
Instead of select * specify what you actually want and use select user_levels.* or even better skip the * and write out the columns you want (and consider using aliases to keep it short and tidy): select ul.col1, ul.col2 ... from userlevels ul join ...
It is getting all the data as the '*' means 'all' columns. You can limit the columns for just one table by specifying the table:
select user_levels.*
from user_levels
join collectors_users on user_levels.id = collectors_users.user_level
where collectors_users.username = 'testuser'
Pro tip: Don't use SELECT * in running software. Instead, be as specific as you can be about the columns you want in your result set.
SELECT user_levels.*
should help a bit.
I might suggest that you use in or exists, because this is more consistent with the intention of the query:
select ul.*
from user_levels ul
where ul.id in (select cu.user_level
from collectors_users cu
where cu.username = 'testuser'
);
In addition, this version will not produce duplicate rows if collectors_users has multiple matching rows for a singel row in user_levels.
Also note the use of table aliases: these make the query easier to write and to read.
I have been trying to use AND in my SELECT, but I got an error.
What is likely the correct way I should have written the code?
The code is:
$sel=mysql_query("SELECT * from student, subject, studentmark
where student.username='$name' AND studentmark.student_id='$name' AND studentmark.YEAR='$ya' AND studentmark.Term='FIRST' AND studentmark.Term='SECOND' AND studentmark.Term='THIRD' AND subject.code=studentmark.code AND student.username=studentmark.student_id");
A simple rule: Never use commas in the FROM clause. Always use explicit JOIN syntax. The result is something like this:
SELECT *
from studentmark sm join
student st
on sm.student_id = st.username join
subject join
on sm.code = su.code
where st.username='$name' AND sm.YEAR = '$ya' AND
sm.Term in ('FIRST', 'SECOND', 'THIRD')
Also, learn to use table aliases. They make queries easier to write and to read.
You must use OR instead of AND
studentmark.Term='FIRST' AND studentmark.Term='SECOND'
The Value of Term can only has ONE Value
I don't know what columns do you want to get from student, subject & studentmark tables & also relationship of those tables. So here is my idea:
Use OR instead of AND on the same column check: studentmark.Term='FIRST' AND studentmark.Term='SECOND' AND studentmark.Term='THIRD'. This will return nothing.
Use table join query instead of: subject.code=studentmark.code AND student.username=studentmark.student_id, it is not correct. Take a look here: SQL Joins
I am trying to create a new data extract from a (badly designed) sql database. The customer requires that I add a distinctidentifier which I am attempting to do using the NEWID() function. Unfortunately this leads to multiple duplicate records being returned.
After a bit of research I have found that the NEWID() function does indeed 'undo' the use of the distinct keyword, but I cannot work out why or how to overcome this.
An example of the query I am trying to write is as follows:
select distinct
NEWID() as UUID
,Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)
If I miss out the NEWID() line in the select block, I get 569 records returned, which is correct, but if I include that line then I get in excess of 30,000 which are all duplicates of the original 569 but with different IDs. Can anyone suggest a way around this problem?
Thanks in advance
Use a sub query would be the easiest way to do it.
SELECT NEWID() as UUID
, * -- this is everything from below
FROM (
select distinct
Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)) as mySub
select NEWID() as UUID
,ISRN
,Internal_Patient_No
,Date_of_Birth
,histo_report
,Investigation_Result_Date
from (
select distinct
,Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)) t
You can use a sub-query to get around the issue, something like.....
SELECT NEWID() as UUID
,*
FROM (
select distinct
Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report
on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)
) t