user1494854
user1494854

Reputation: 171

mysql getting data from a table if it has a value in another table

I have two mysql tables

myTabs

  1. id
  2. usercode
  3. tab_id
  4. fid
  5. tablistid

Tabs

  1. tab_id
  2. title
  3. usercode
  4. access_type
  5. question

both tables have tab_id as primary key and use the same value. I want to list the data from mytabs table which has same tab_id in Tabs Table and access_type = 1. I dont want to list the records from Tabs the query should only validate from Tabs if that Tab has access_type 1 then it should list

Is it possible? What I am trying is it has not returned anything.

 $mysql = "select  mytabs.*, tabs.* FROM mytabs, tabs  where mytabs.usercode='$usercode' and (mytabs.fid IS NULL || mytabs.fid='0') and tabs.access_type = '1' order by mytabs.tablistid asc"

Upvotes: 1

Views: 113

Answers (2)

Dipesh Parmar
Dipesh Parmar

Reputation: 27382

You can use INNER JOIN.

select * from tabs INNER JOIN myTabs ON tabs.tab_id = myTabs.tab_id

and append other condition in where clause.

And what INNER JOIN do is return rows only when there is at least one row from both tables that matches the join condition.

Upvotes: 2

Mudassir Hasan
Mudassir Hasan

Reputation: 28771

Use this query

SELECT * FROM Shortcut s
INNER JOIN Tabs t
ON s.tab_id=t.tab_id
WHERE t.access_type=1 AND t.usercode = '$usercode'

Upvotes: 1

Related Questions