0

For Example, I have 2 tables.

Table A has Setting/isActive

Table B has Name/Ages/City

So I want to retrieve records from B only when isActive = 1 for Setting ='GetThis' in Table A

something like

if 'GetThis' = 1 from table A
then get records from table B
Pandakkk
  • 29
  • 3
  • 1
    Get what records from B? there does not appear to be a relationship between A and B – P.Salmon Mar 29 '22 at 19:11
  • get all records from B, it doesn't matter what records, and there is no relationship between A and B, but I want get records from B if condition in A is true – Pandakkk Mar 29 '22 at 19:23
  • @Pandakkk, does Table A has column name too? based on your question above, `if 'GetThis' = 1 from table A` What records from Table B should it gets? – fonz Mar 30 '22 at 01:36
  • Please read https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html, https://dev.mysql.com/doc/refman/8.0/en/if.html , https://stackoverflow.com/questions/5528854/usage-of-mysqls-if-exists try something and if you get into difficulty post your code. – P.Salmon Mar 30 '22 at 07:05

3 Answers3

0

Don't know which DBMS you're using, but look into the command IF EXISTS

StackGesus
  • 11
  • 4
0

If it should get all records from table B, you can try this;

Select b.name, b.ages, b.city from table_B b, table_A a where a.isActive = 1 and a.Setting ='GetThis' 
fonz
  • 167
  • 8
0

I ended up doing this

CREATE PROCEDURE GetAllRecords
@isActive BIT
SET @isActive = (Select isActive FROM TableA WHERE Setting = 'GetThis')

AS

IF @isActive = 1
BEGIN

SELECT * FROM TableB

END
GO;
Pandakkk
  • 29
  • 3