MySQL join with 2 conditions error - mysql

I can't figure out why I get an SQL error with the mySQL statement (in php file) below. I think the problem relates to the second condition 'AGREEDPRODUCTS.productid_corporation is null'. I checked that the syntax of the used parameters is correct as used in the database. I also tried other alternatives with respect to the second condition (like using WHERE but that is obvious not allowed;MySQL join with where clause) but those didn't work.
$sqlquery4 = "SELECT AGREEDPRODUCTS.id,AGREEDPRODUCTS.productid_supplier,EMETERPRODUCTS.productname "
. "FROM AGREEDPRODUCTS "
. "INNER JOIN EMETERPRODUCTS "
. "ON AGREEDPRODUCTS.productid_supplier=EMETERPRODUCTS.productid AGREEDPRODUCTS.producttype='EMETER' AND AGREEDPRODUCTS.productid_corporation is null "
. "ORDER BY AGREEDPRODUCTS.productid_supplier";
Any suggestions?

You've missed an AND, or other separator, on that line:
ON AGREEDPRODUCTS.productid_supplier=EMETERPRODUCTS.productid AGREEDPRODUCTS.producttype='EMETER' AND AGREEDPRODUCTS.productid_corporation is null
^^^

ON AGREEDPRODUCTS.productid_supplier=EMETERPRODUCTS.productid AND AGREEDPRODUCTS.producttype='EMETER' AND AGREEDPRODUCTS.productid_corporation is null
You're missing an AND........................................................................................^here^.
And you confused something here:
like using WHERE but that is obvious not allowed
The WHERE in this question makes the left/right join to an inner join. That's why you have to put it in the join condition. A WHERE clause is always possible. You could as well also put your second join condition in a where clause.

Related

Transact SQL trying to avoid duplicates using inner joins and "Like" constraint

SELECT numSerie, nomVehicule,model,AnneeModel
FROM Vehicule,Marque where Vehicule.idMarque = Marque.idMarque and Vehicule.numSerie
LIKE "%" OR Marque.nomVehicule LIKE "%"
this display something like this, it duplicates results
result
if you remove this part of code it works just fine
OR Marque.nomVehicule LIKE "%"
result 2
Your WHERE clause needs some work. Look at the boolean logic. When you don't enclose conditions in parenthesis, it evaluates the boolean operators left-to-right.
Switching to ANSI-92 SQL notation would solve the problem immediately. ANSI-92 notation is 25 years old and while database engines still run queries with older join notation just fine, using ANSI-92 join notation would have avoided this problem completely.
FROM Vehicule,Marque
WHERE (Vehicule.idMarque = Marque.idMarque)
AND ((Vehicule.numSerie LIKE "%") OR (Marque.nomVehicule LIKE "%"))
Or even better, in ANSI-92 notation::
FROM Vehicule
JOIN Marque ON (Vehicule.idMarque = Marque.idMarque)
WHERE Vehicule.numSerie LIKE "%" OR Marque.nomVehicule LIKE "%"

Multiple Joins with And Statement not working

I can not seem to get this to work as expected.
"SELECT event_positions.id as ep_id, event_positions.pos_prefered_tech, event_positions.assigned_tech_id, "
. "event_schedule.id as es_id, event_schedule.event_id, event_schedule.event_day, event_schedule.event_stime,"
. "event.id as eid, event.crewer_id as cid, event.event_title, event.crewed_by,"
. "crewer.crewer_company"
. "FROM event_schedule "
. "INNER JOIN event_positions "
. "ON event_schedule.id = event_positions.event_sched_id"
. "INNER JOIN event "
. "ON event_schedule.event_id = event.id"
. "INNER JOIN crewer "
. "ON event.crewer_id = crewer.id "
. "WHERE event_schedule.event_day >= NOW() "
. "AND event.crewer_id = ?"
If i remove the AND statement it will pull all data as expected. But I need to filter for the specific crewer_id
When I try to do this I get an empty result set. No errors.
It seems like there's some spaces missing in the generated SQL text, for example, before FROM and before INNER JOIN crewer. Are you sure this SQL statement is working?
The question mark character ? doesn't look like valid SQL. So it's likely (and we're going to assume) that this SQL text is for a prepared statement, and that the question mark is intended as a placeholder for a bind variable.
If that's the case, I suspect there's a problem with the parameter bind.
I recommend you verify that the value you are providing for the bind parameter is a value that would return rows, that is, one of the values for crewer.id that's returned by the query when this predicate is omitted.
I also suggest you test using a hardcoded literal value, in place of the question mark. Choose a literal value, again, that you know will return rows.
I suspect that when you debug this, you will find the problem is with the bind parameter. (It's only a suspicion, because there's not enough information provided for me to make a determination.)

Why isn't my SQL LEFT JOIN query working?

It is returning 0 rows when there should be some results.
This is my first attempt at using JOIN but from what I've read this looks pretty much right, right?
"SELECT pending.paymentid, pending.date, pending.ip, pending.payer, pending.type, pending.amount, pending.method, pending.institution, payment.number, _uploads_log.log_filename
FROM pending
LEFT JOIN _uploads_log
ON pending.paymentid='".$_GET['vnum']."'
AND _uploads_log.linkid = pending.paymentid"
I need to return the specified values from each table where both pending.paymentid and _uploads_log.log_filename are equal to $_GET['vnum]
What is the correct way to do this? Why am I not getting any results?
If someone more experienced than me could point me in the right direction I would be much obliged.
EDIT
For pending the primary key is paymentid, for _uploads_log the primary is a col called log_id and log_filename is listed as index.
Try this
SELECT pending.paymentid,
pending.date,
pending.ip,
pending.payer,
pending.type,
pending.amount,
pending.method,
pending.institution,
payment.number,
_uploads_log.log_filename
FROM pending
LEFT JOIN _uploads_log
ON _uploads_log.linkid = pending.paymentid
WHERE _uploads_log.log_filename = '" . $_GET['vnum'] . "'
Your current query is vulnerable with SQL Injection. Please take time to read the article below.
Best way to prevent SQL injection in PHP?
The ON clause only should have the condition to link the two tables especially if it is LEFT JOIN. The WHERE clause then has the actual condition. Otherwise you will get nothing if there is no corresponding entry in _uploads_log. It also is more easy to read in my opinion.
As another remark. It is always better to work with bind parameters to avoid SQL injection.

LEFT JOIN breaks WHERE Clause

I've recently been required to input more information from my database and I've just LEFT JOIN to help me, it works almost perfectly(it does actually get the right field from the other table) but my WHERE clause is nullified giving the user access to both tables without the restriction of my where clause.
MySQL doesn't crap out any errors, so I'm assuming it's something to do with my where clause or something happened in the join.
SELECT * FROM students
LEFT JOIN courses ON students.appliedforCourse = courses.idNumber
WHERE
students.telephone LIKE '%$var'
OR students.email LIKE '%$var'
OR students.address like'%$var%'
OR (CONCAT(students.firstName,' ',students.lastName) LIKE '%$var%')
AND addedBy ='$userid'
LIMIT $s,limit
The query itself is correct (although really inefficient due to ORs and % % [ indexes will not be used] ).
I would suggest to echo the query, are you sure that $var is evaluated correctly ? Try to run the query directly in mysql (via phpmyadmin for example or using console).
I suspect that simply you did not set $var value. Then condition e.g. students.telephone LIKE '%$var' will become students.telephone LIKE '%' (always true for not null address), which will match every record of the join , exactly what you are getting.

Ambiguous left join?

so I'm struggling with what I'm guessing is a very simple problem. I've searched around a bit, but none of the solutions I've found so far have worked on my problem.
SELECT arrangement_ID, hva, dato
FROM tt_arrangement LEFT JOIN (tt_vaktliste_vakt)
ON (tt_arrangement.arrangement_ID = tt_vaktliste_vakt.arrangement_ID)
This naturally produces the "ambiguous error", since the column 'arrangement_ID' is present in both tt_arrangement and tt_vaktliste_vakt. Thinking this was easy to fix, I made the following changes:
SELECT **arrangement_ID.tt_arrangement**, hva, dato
FROM tt_arrangement LEFT JOIN (tt_vaktliste_vakt)
ON (tt_arrangement.arrangement_ID = tt_vaktliste_vakt.arrangement_ID)
However, this produced the error "column doesn't exist". And that's where I'm stuck.
Not sure if it matters, but when using SELECT * the query works as intended. Though that is not really an option for what I'm going to use the query for.
In advance, thanks for any replies.
Prefix the ambiguous column name with the tablename:
SELECT t_arrangement.arrangement_ID, hva, dato
FROM tt_arrangement LEFT JOIN (tt_vaktliste_vakt)
ON (tt_arrangement.arrangement_ID = tt_vaktliste_vakt.arrangement_ID)
(assumes hva, dato are unique column names)
(You can also use aliases, but will still need to prefix ambiguous column names with alias)
You need to give your table names an alias, like below.
SELECT a.arrangement_ID, a.hva, a.dato
FROM tt_arrangement AS a
LEFT JOIN tt_vaktliste_vakt AS v
ON (a.arrangement_ID = v.arrangement_ID)
Not sure is the top lines right as i don't know you table structure so can't know which column comes from where.
Hopes this helps.