473,782 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2359
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=table 2.id
WHERE table2.id IS NULL;
--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 23 '05 #2

"Chris Hope" <bl*******@elec trictoolbox.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=table 2.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
siteadministrat or 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
siteadministrat or 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_${a lbumname}"
VALUE=$is_check ed>
</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
15005
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 using this technique is: update ( select columnName, value from name, lookup
7
9228
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 a table (c) re-queries another table using a subquery which references the inserted table (correlated or not)
3
13369
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 just won't work. When running columnAlias IS NOT NULL i just get the error "Invalid column name 'columnAlias'. This is the query: SELECT k.UserId, k.Lastname, k.Firstname, (SELECT kscr.answer FROM Results kscr WHERE kscr.UserID =
8
19601
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 couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two fields from a *single* record from a table called 'Locations' and then apply one of these field's values...
7
2370
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 which a part may be composed. I have a table of parts and their subparts. The problem is that each of those subparts may be composed of smaller component parts. The subpart would then be listed in the Part field linked to each of its subparts in...
5
2553
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 customers linked to a table of customer contacts. The contact table has a field called 'type of contact'. Contact types can be numeric 1 to 40 and show the different types of contact I have with my customers (e.g. 1 - initial contact, 2 -...
3
3517
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 (PrimaryKey) - Quote I do some calculations on these data:
4
8597
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
4372
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 Student IDs are linked. I'm trying to see the Student ID that has occured most in the LESSON_TABLE. I've figured that, the way I should do this is to use the Count Function on the . and then the Max function on that. But I don't know how to do...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.