473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

subquery without a subquery

I've the following problem:

Table1
serie | dir

Table2
serie | user

I am making a webpage in php to administrate the values in the db.
What I want is that I select all series from table 1 with an checkbox and
that the ones that are in table 2 will have a checked checkbox.

with the mysql running on the server it is not possible to do a subquery.
with a subquery it would be something like:
"select serie from table1 where not in (select serie from table2 where
user=user)"

Is there another way to get these results?
Jul 23 '05 #1
3 2332
Maarten wrote:
I've the following problem:

Table1
serie | dir

Table2
serie | user

I am making a webpage in php to administrate the values in the db.
What I want is that I select all series from table 1 with an checkbox
and that the ones that are in table 2 will have a checked checkbox.

with the mysql running on the server it is not possible to do a
subquery. with a subquery it would be something like:
"select serie from table1 where not in (select serie from table2 where
user=user)"

Is there another way to get these results?


From the manual at http://dev.mysql.com/doc/mysql/en/JOIN.html :

If there is no matching record for the right table in the ON or USING
part in a LEFT JOIN, a row with all columns set to NULL is used for the
right table. You can use this fact to find records in a table that have
no counterpart in another table:

SELECT table1.* FROM table1
LEFT JOIN table2 ON table1.id=table2.id
WHERE table2.id IS NULL;
--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 23 '05 #2

"Chris Hope" <bl*******@electrictoolbox.com> schreef in bericht
news:11***************@216.128.74.129...
Maarten wrote:
I've the following problem:

Table1
serie | dir

Table2
serie | user

I am making a webpage in php to administrate the values in the db.
What I want is that I select all series from table 1 with an checkbox
and that the ones that are in table 2 will have a checked checkbox.

with the mysql running on the server it is not possible to do a
subquery. with a subquery it would be something like:
"select serie from table1 where not in (select serie from table2 where
user=user)"

Is there another way to get these results?


From the manual at http://dev.mysql.com/doc/mysql/en/JOIN.html :

If there is no matching record for the right table in the ON or USING
part in a LEFT JOIN, a row with all columns set to NULL is used for the
right table. You can use this fact to find records in a table that have
no counterpart in another table:

SELECT table1.* FROM table1
LEFT JOIN table2 ON table1.id=table2.id
WHERE table2.id IS NULL;
--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

This isn't working I'll try to explain it better..

What I am talking about is a login page with photoalbums. Not everyone has
access to all albums.

In total if have 3 tables
-The first one has all the usernames and passwords for access to a specific
page.
-The second one has all the albumnames and the directories on the server
stored in it.
-The third table contains which user has access to which album.

Table1 = user table:
user | password
------------------------
user1 | password1
user2 | password2
user3 | password3
etc....

Table2 = albumstable
albumname | directory
--------------------------
summer | summer2004
winter | winter2003
birthday | birthday
etc...

Table3 = connection between users and albums.
user | album
--------------------------
user1 | summer
user1 | birthday
user3 | winter
user2 | birthday
user2 | winter
etc....

if your username is in table 3 you have access to this album.

What I want to be able to do is the following:
I want to be able to select all the series that are in table2 from the db
and put the output on a page with checkboxes next to each serie.
If the user that I am modifiing has access it should be checked and if the
user hasn't got access to this album it should be unchecked. As the
siteadministrator I want to be able to check or uncheck each line and put
that back in the database.

I hope you understand my question better know.

Maarten.
Jul 23 '05 #3
Maarten wrote:
I want to be able to select all the series that are in table2 from the db
and put the output on a page with checkboxes next to each serie.
If the user that I am modifiing has access it should be checked and if the
user hasn't got access to this album it should be unchecked. As the
siteadministrator I want to be able to check or uncheck each line and put
that back in the database.


This isn't just a SQL question, this is a web programming question.
Fetching the data is relatively easy:

SELECT A.albumname, IF(C.user IS NULL, 1, 0) AS is_checked
FROM albumstable AS A LEFT OUTER JOIN connection AS C
ON (A.albumname = C.album AND C.user = ?);

You need to output an HTML form with a list of the albums, and a
checkbox, checked if "is_checked" is 1.

<FORM>
<INPUT TYPE=hidden NAME=user VALUE=$user>
Loop:
$albumname
<INPUT TYPE=checkbox NAME="album_${albumname}"
VALUE=$is_checked>
</FORM>

In your code that processes the form, you need to figure out how to tell
the difference between old data and new data.

One method would be to delete _all_ the rows in Table3 for that user,
and then insert new rows for the checked album values found in the form.

$user = input parameter "user"
DELETE FROM connection WHERE user = $user;

Loop over input parameters matching "album_*":
if value of parameter is 0 (not checked), skip to next in loop.
$album = name of parameter, removing "album_" prefix.
INSERT INTO connection (user, album) VALUES ($user, $album);

It is recommended to do this series of statements in a transaction (if
you use InnoDB tables), so that if the INSERT fails for any reason, then
the DELETE should be rolled back.

Regards,
Bill K.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Murali | last post by:
Hi All I was reading thro the posting(s) of Thomas Kyte and his nifty approach to doing updates without the need for unnecessary correlated subqueries. An alternative to correlated subquery...
7
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into...
3
by: olanorm | last post by:
I have a query where one or more of the columns returned is a result from a subquery. These columns get their own alias. I want to filter out the rows containing NULL from the subqueries but it...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
7
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
5
by: redstamp | last post by:
Try as I might I cannot find a way to write an access query to return a result set with the records from my database WITHOUT a certain set of values within a field. To explain, I have a table of...
3
by: laurentc via AccessMonster.com | last post by:
Hi. I have an issue with my Access project. I have rather big tables of data (about 11 000 rows). These tables are historical product quotations, so they are very simple : - MyDate...
4
by: tathagata | last post by:
I want second highest value in a table without using subquery. please send this answer .It will be very helpfull.
4
by: James Tran | last post by:
Hi, I've been trying to find and display only the maximum count of a field. I have two tables; STUDENT_TABLE - Student ID, Title, Surname and Christan Name. LESSON_TABLE - Student ID The two...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.