Have 3 tables called "table1" "table2" and "table3". Only the column id is same in these tables. All other columns are different.
Will select like:
"select * from table1, table2, table3 where WHERE (date >= now())";
I am facing problem while echoing the data. The table1 have one column called "org", table2 have "name", table3 have "pgm". I want something like:
echo "$data['org']" OR $data['name'] OR $data['pgm']"
Is this possible?
So, assuming what you said is true:
Only the column id is the same in these tables
Then you can join the tables that share this column value and retrieve the values back in a query:
SELECT t1.org,
t2.name,
t3.pgm
FROM table1 t1
INNER JOIN table2 t2
ON t2.id = t1.id
INNER JOIN table3 t3
ON t3.id = t1.id
WHERE t1.date >= NOW();
An explicit join, but further illustrates the relationship the tables must have when collecting data from different sources.
Related
The database I am working on right now is somewhat messy. I have three potential tables, that I want to join but in some cases it may only be two tables.
Let's call these table1, table2 and table3.
table1 has a field called "type". If table1.type is 2, then I only need to join table3.
For any other values I want to join table2 and then table3.
How can I achieve this in one single SQL query rather than: 1) having one query to select the type. 2) make a PHP foreach-loop to check the type of the current iteration and 3) perform a new query according to the type value.
Edit:
I'll try to be more specific.
table1 has a column named "pid" that references to a whole other table, but that's redundant to this question. I tried working my ways around with UNIONs and LEFT JOINs but couldn't manage to achieve what I was looking for.
I want to select all results from my database with the "pid" value being "100". This gives me four rows in return, where was 2 of them are of type value "2" and the others are "1".
So basically what I want to achieve is the following two SQL statements in one:
(If "type" is "2")
SELECT *
FROM table1 t1
INNER JOIN table3 t3
ON t1.id = t3.t1_id
WHERE t1.pid = 100
(If "type" is NOT "2")
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.t1_id
INNER JOIN table3 t3
ON t2.id = t3.t2_id
WHERE t1.pid = 100
I'm guessing I could manage to do this with a UNION statement, but I'm confused on how to implement the WHERE t1.pid = '100' part.
use an UNION e.g.
SELECT t1.*, t3.*
FROM table1 t1
INNER JOIN table3 t3 ON t1.id = t3.t1_id
WHERE t1.pid = 100 and t1.type = 2
UNION
SELECT t1.*, t3.*
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.t1_id
INNER JOIN table3 t3 ON t2.id = t3.t2_id
WHERE t1.pid = 100 and t1.type <> 2;
but it would be better to explicitly name the columns you want to get back.
I have two database tables - table1 and table2. For some records in table1 i have several rows connected in table2. For most of them i have 3 rows connected, but for some of them i have an extra row with a column value like table2.field='correct'. How can i join table1 and table2 if i want the result to return only the rows from table1 where there is no row in table2 with column value like table2.field='correct' connected to them ? Counting the number of rows from the second table( if num of connected rows < 4 or something like that is not an option).
I tried something like :
SELECT t1.* FROM table1 t1 LEFT JOIN table2 t2 on t1.id=t2.id_t1 WHERE t2.field IS NULL
but it did not work ofc. because i always have rows with value in field column. For each row in t1 that is connected to t2 i have record rows in t2 where t2.filed='name' and t2.field='type'. I need the rows from t1 that do not have a row connected to them in t2 where t2.field='correct'.
Use NOT IN
SELECT * from t1 WHERE t1.id NOT IN(SELECT id FROM t2 WHERE t2.field = 'correct')
$sql = "SELECT t1.* FROM table1 t1 CROSS JOIN table2 t2 on t1.id=t2.id_t1 WHERE t2.field IS NULL ";
SELECT t1.* FROM table1 t1 WHERE NOT EXISTS (SELECT t2.* FROM table2 t2 WHERE t1.id=t2.id_t1 AND t2.field = 'correct')
Table1 Has Some data as Categories
Table2 Has Some data Which is Realated to table1 Categories
and the relation between two tables is cat_id from table1 and cat_ids from table2.
What I want is?
I need to display all fields in table1 and from table2 I need only the related content i.e id's present in cat_id(table1) and cat_ids(table2)
I am using a query like this select c.* ,cc.* from news_categories cc, news_content c where cc.cat_id = c.cat_ids group by cc.cat_id this gives only common data from table1 and table2.. i need common data and all categories from table1
can anyone help me?
You should use JOIN instead.
SELECT t1.*, GROUP_CONCAT(t2.content_id)
FROM table1 t1
LEFT JOIN table2 t2
ON t2.cat_ids = t1.cat_id
GROUP BY t1.cat_id
This is for all fields of both tables...
SELECT Table1.*, Table2.*
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_ids
This is for all fields of Table1 and Content field of Table2...
SELECT Table1.*, Table2.full_content
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_ids
you need to use outer join and this case left join .. Have a look at Documentation
I have a problem...I have 2 tables, Table1 and Table2.
Table1:
id,int(11)
text,varchar(11)
Table2:
id,int(11)
agentid,int(11)
unique(id,agentid)
Table2 has many id and agent ids in it. The data for the id in table 2 came from table 1.
I have an agentid say $aid. This has many id's associated with it in table 2.
I am trying to get the set of text values out from table 1 associated with all ids which are related to agentid $aid from table 2.
Does that make sense?!
Any ideas?
select text from table1 where id in
(
select id from table2 where agentid = <your aid>
)
This will give you all text rows for given agentid.
The same can be done using join too -
select t1.text from table1 t1
inner join table2 t2
on t1.id = t2.id
where t2.agentid = <your aid>
select * from table1 as t1 inner join table2 as t2
on t1.id = t2.id
where agentid = $aid
The query:
$sql = "select t1.text from table1 t1, table2 t2 where t2.id = t1.id and t2.agentid = {$aid}";
Try not to include the $aid directly in the query, but escape it and/or use prepared statements with query parameters.
I have to three table I need to gather data from in a search process:
Commissions Table - Table1: [affiliate_id]
Affiliates Table - Table2: [id][user_id]
Profiles Table - Table3: [ID][NickName]
The search input I'll have is someone searched for a username. I need to return the data from table 1 where the affiliate_id matches the user_id of Table2, that is like the nickname that will be searched for.
I hope that makes sense :)
Try this:
"select table1.* from table1
inner join table2 on table2.user_id = table1.affiliate_id
inner join table3 on table3.id = table2.user_id
where table3.nickname like '%".mysql_real_escape_string($searchtext)."%'"
SELECT t1.*, t3.nickname FROM Table1 t1
JOIN Table2 t2 ON t2.id=t1.affiliate_id
JOIN Table3 t3 ON t2.user_id=t3.user_id
WHERE t2.user_id=?;