PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 1, 2022

[FIXED] Select all elements from a table, and check if they match to another table

 February 01, 2022     mysql, phpmyadmin, select, sql     No comments   

Issue

I have two tables, and I want to take all entities from the first table, then, check if they can be related to a specific entity from another table. If they can be related the database returns 1 (or true), else it returns NULL (or false).

Aïe

I tried some things with LEFT JOIN but none of them work. I think the solution is simple but I can't figure it out...

Context : In my application I make two requests, the first one take all entities from oneTable, the second one take all idOT from anotherTable where idAT is equal to 2, THEN, I make a loop where I save all entities from the first request and in this loop I make another loop where I check if the current element is present on the second request. I thought they this solution was to heavy (two requests and imbricated loops) so I tried to do it directly in one request.

Thank you for your help guys ! I hope it won't make you lose your time...

Edit : @Strawberry gave me the answer in the comments, I was doing

SELECT * FROM oneTable LEFT JOIN anotherTable ON oneTable.idOT = anotherTable.idOT **WHERE** anotherTable.idAT = 2

instead of

SELECT * FROM oneTable LEFT JOIN anotherTable ON oneTable.idOT = anotherTable.idOT **AND** anotherTable.idAT = 2 

It's as simple as that... Thank you again guys.


Solution

Thank to the comments of the question the correct request is :

SELECT * FROM oneTable
LEFT JOIN anotherTable ON oneTable.idOT = anotherTable.idOT
AND anotherTable.idAT = 2;


Answered By - Rofez
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing