How to use the select AND - mysql

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

Related

In mysql, can ON statement in an INNER JOIN accept an OR clause?

Can an ON clause of an INNER JOIN accept an AND?
In the following (and presently working) mysql example:
SELECT p.pk_ProductID
FROM Product p
INNER JOIN SerialNumber sn
ON sn.fk_ProductID = p.pk_ProductID
WHERE sn.pk_SerialNumberID = %s AND p.ProductGUID = %s
LIMIT 1
Is it legit to add an OR clause with sn.fk_ProductID2 like so:
ON sn.fk_ProductID = p.pk_ProductID OR sn.fk_ProductID2 = p.pk_ProductID
If legit, would it be stylistically more readable to be in parenthesis:
ON (sn.fk_ProductID = p.pk_ProductID) OR (sn.fk_ProductID2 = p.pk_ProductID)
NOTE: I have reviewed several seemingly similar questions on SO which contained conflicting advice re part 1 of my question.
It's legal to write a query like you show. The expression following the ON keyword can be any boolean expression. Strictly speaking, it doesn't even have to reference either table you are joining.
It is not necessary to use parentheses, because the operator precedence of = versus OR is clearly that = binds tighter. See https://dev.mysql.com/doc/refman/8.0/en/operator-precedence.html for details on that.

MYSQL Error 1060 Duplicate

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.

How to sum all elements in an indexed table

I tried to make a query to my database with this structure: Data Base Structure
I did this query:
SELECT partido.acronimoPartido, SUM(votosacta.numVotos) FROM partido, votosacta WHERE votosacta.partido_idpartido=1 AND partido.idpartido=1
This query does work like I want but displays only the SUM of 'votos' for idpartido=1
I want to be able to sum numVotos from 'votosacta' table for each member of my 'partido' table indexed in 'votosacta' but I seem to not be able to get the right sintax.
I tried something like this:
SELECT partido.acronimoPartido, SUM(votosacta.numVotos) FROM partido, votosacta WHERE votosacta.partido_idpartido = partido.idpartido
You need a group by clause:
select p.acronimoPartido,
SUM(v.numVotos)
from partido p
join votosacta v on v.partido_idpartido = p.idpartido
group by p.acronimoPartido
Also, use explicit join syntax instead of old comma based syntax and use aliases to make your queries concise and readable.

Sql query selecting from both tables.

I have a query which looks like this
$db->query("SELECT A.page_id, A.page_name FROM user_likes_pages as A , user_likes as B WHERE A.page_id = B.page_id AND B.country_id = ".$user_reg." ");
The thing is, I want to select a column from user_likes. Do I have to make a join or I can do it in different way. Thank you.
You have a join in your query, but it is implicit. You should write the query as:
SELECT ulp.page_id, ulp.page_name, ul.<whatever>
FROM user_likes ul JOIN
user_likes_pages ulp
ON ul.page_id = ulp.page_id
WHERE ul.country_id = ".$user_reg."
In addition to adding the explicit join syntax, I also changed the table aliases so they are abbreviations of the table name. This makes it easier to read the query and avoid mistakes.
You select B.columnname. You don't need a join, because you have the A.page_id = B.page_id.

MySQL query - multiple having statements not working

I'm trying to use the following query, and if it only has one having statement it works as expected. If I add the second having statement it does not work.
SELECT candidate.first_name,
candidate.last_name,
qualification.code,
property.value AS funding_band_value,
qualification.funding_band,
property.value AS qualification_level_value,
qualification.qualification_level_id
FROM candidate_qualification, candidate, qualification, property
WHERE candidate_qualification.candidate_id=candidate.id and
candidate_qualification.qualification_id=qualification.id
HAVING funding_band_value = (select property.value from property where qualification.funding_band=property.id) and
HAVING qualification_level_value = (select property.value from property where qualification.qualification_level_id=property.id)
Could someone explain why this doesn't work and how I should do this.
HAVING acts similarly to WHERE or GROUP BY. You reference it once to start using it and combine multiple statements with AND or OR operators. An in depth look at the query parser might give you a more explicit answer.
You don't need HAVING here, just use AND so it is part of your WHERE clause.
The subqueries are not necessary, those tables are already joined.
Something like this should be closer to what you want:
SELECT c.first_name,
c.last_name,
q.code,
p.value AS funding_band_value,
q.funding_band,
p.value AS qualification_level_value,
q.qualification_level_id
FROM candidate_qualification cq
INNER JOIN candidate c ON cq.candidate_id=c.id
INNER JOIN qualification q ON cq.qualification_id=q.id
INNER JOIN property p ON q.funding_band=p.id
and q.qualification_level_id=p.id