我有两个表table1和table2。
table1 - tID,title,description
table2 - sID,tID,title,description
table1(t1)始终包含值。 在table2(t2)中,如果存在一行,则tID(外键)将始终与t1(主键)中的tID匹配。
我想创建一个select语句,我在其中选择t1中的所有列,并创建一个带有true或false值的附加列“ex”,具体取决于t2中是否有记录,其中tID匹配... < / p>
Select t1.*, (some code) as ex from table1 t1,table2 t2;
明白了吗?例如,如果Select t1.*, t2.* from table1 t1, table2 t2 where t1.tID = t2.tID
返回任何行,ex同样应该是真的...我该怎么做?
编辑:没有列可以为空
编辑:忘记提及,我希望select语句只匹配我指定的sID = someIdValue ....
答案 0 :(得分:1)
使用 <html>
<head><title>Add info...</title></head>
<body>
<form action="tfs.php" method="post">
<table>
<tr>
<td>First Name :</td>
<td><input type="text" name="fsname" /></td>
</tr>
<td>Last Name :</td>
<td><input type="text" name="lsname" /></td>
</tr>
<tr>
<tr>
<td>rent on :</td>
<td><input type="text" name="rent_on" /></td>
</tr>
<tr>
<td>room status :</td>
<td><input type="text" name="status" /></td>
</tr>
<td>city :</td>
<td><input type="text" name="city" /></td>
</tr>
<tr>
<td>area :</td>
<td><input type="text" name="area" /></td>
</tr>
<tr>
<td>college :</td>
<td><input type="text" name="college" /></td>
</tr>
<tr>
<td>room for :</td>
<td><input type="text" name="rent_for" /></td>
</tr>
<tr>
<td>quantity :</td>
<td><input type="text" name="quantity" /></td>
</tr>
<tr>
<td>bed status :</td>
<td><input type="text" name="bed" /></td>
</tr>
<tr>
<td>study table :</td>
<td><input type="text" name="s_table" /></td>
</tr>
<tr>
<td>wifi :</td>
<td><input type="text" name="wifi" /></td>
</tr>
<tr>
<td>gizer :</td>
<td><input type="text" name="gizer" /></td>
</tr>
<tr>
<td>Contact :</td>
<td><input type="text" name="contact" /></td>
</tr>
<tr>
<td>password :</td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td>mail id :</td>
<td><input type="text" name="mail_id" /></td>
</tr>
<tr>
<td>Address :</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>postal code :</td>
<td><input type="text" name="pcode" /></td>
</tr>
<tr>
</table>
<input type="submit" value="Submit Info" />
</form>
</body>
</html>
the connection between database is successful but some values like pcode, geo, rent shows doesn't have default values. The error is as shown in image.
(外部联接),然后使用LEFT JOIN
表达式检查tID
中是否存在table2
IS NULL
编辑:&#34;如果我只想要sID匹配某些值的值,该怎么办?&#34;
Select t1.*, t2.tid is not null as ex
from table1 t1
left join table2 t2 on t1.tid = t2.tid
答案 1 :(得分:1)
试试这个
select *, coalesce(tID = true, false) = true as is_exists from table1 t1
left join table2 t2 using (tid)
答案 2 :(得分:1)
如果这对你有用,请尝试下面的一个。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="appName_list">
<div class="appName_main" data-type="header" data-id="1">
Header Data
</div>
<div class="appName_main" data-type="header" data-id="2">
Header Data Two
</div>
<div class="appName_main" data-type="footer" data-id="3">
Footer Data
</div>
<div class="appName_main" data-type="header" data-id="4">
Footer Data Two
</div>
<div class="appName_main" data-type="offers" data-id="5">
Offers Data
</div>
<div class="appName_main" data-type="offers" data-id="6">
Offers Data Two
</div>
</div>
<div class="Menu_list">
<div class="appNameSelect" data-type="header" data-id="1">
Header One
</div>
<div class="appNameSelect" data-type="header" data-id="2">
Header Two
</div>
<div class="appNameSelect" data-type="footer" data-id="3">
Footer One
</div>
<div class="appNameSelect" data-type="footer" data-id="4">
Footer Two
</div>
<div class="appNameSelect" data-type="offers" data-id="5">
Offers One
</div>
<div class="appNameSelect" data-type="offers" data-id="6">
Offers Two
</div>
</div>
<div id="appendTo">
</div>
输出
declare @T1 as Table
(
tId int,
title varchar(25),
description varchar(50)
)
declare @T2 as Table
(
sid int,
tId int,
title varchar(25),
description varchar(50)
)
insert into @t1 (tId,title,description) values(1,'abc','test')
insert into @t1 (tId,title,description) values(2,'abc','test')
insert into @t1 (tId,title,description) values(3,'abc','test')
insert into @t1 (tId,title,description) values(4,'abc','test')
insert into @t1 (tId,title,description) values(5,'abc','test')
insert into @t2 (tId,title,description) values(10,'abc','test')
insert into @t2 (tId,title,description) values(2,'abc','test')
insert into @t2 (tId,title,description) values(3,'abc','test')
insert into @t2 (tId,title,description) values(14,'abc','test')
insert into @t2 (tId,title,description) values(5,'abc','test')
Select distinct t1.*,
case when t2.tid is null then 'False' else 'True' end as ex from @T1 t1
left join @T2 t2
on t1.tId = t2.tId
答案 3 :(得分:0)
EXISTS(subquery)
产生一个可以直接使用的布尔值:
SELECT t1.*
, EXISTS(SELECT13 FROM table2 t2 WHERE t2.tID = t1.tID) AS does_exist
FROM table1 t1
;
这将产生所有来自t1的行,加上一个标志,指示t2中存在一个或多个相应的行。
如果仅想要t2中匹配行的t1记录,请将exists()
移至where子句:
SELECT t1.*
FROM table1 t1
WHERE EXISTS(SELECT13 FROM table2 t2 WHERE t2.tID = t1.tID)
;