Multiple queries as record source for single form? - ms-access

I'm back with another problem. I have a form based on a query with the parameter in the WHERE clause being the combobox of the form. In the detail, it is continuous form view and shows all the matching fields for the combobox(re-queries after update of combobox). There's a textbox and a button at the footer where the user can add new entries for this data.
I need to have basically a mirror of this form right beside it -- so that they can be compared visually. I need two combo boxes side-by side in my header -- with two controls in the detail section that are populated based on the query. I considered using subform to basically create the illusion of this, but I can't have subforms with continuous form view. The idea that I was thinking was to have two queries as the form's record source:
Select value FROM t1 WHERE criteria = me.combo1;
select value as val2 from t1 WHERE criteria = me.combo2;
Example Data (same structure):
**Friends**
**User friend**
Bob Jack
Bob Zach
Bob Mack
John Juan
John Sha'Quan
I would then have in the detail section a 'value' control and a 'val2' control that would be populated from different queries and criteria.
Is this possible?
Or, should I instead just have two subforms, each with a different parent key determined by the value selected in the combobox? I'd prefer it to be the way I listed, but if there is no other option, is this what I'd have to do?

Try this:
SELECT tx.*, ty.*
FROM (Select id, value FROM t1 WHERE criteria = me.combo1) As tx
LEFT JOIN (select id, value as val2 from t1 WHERE criteria = me.combo2) As ty
ON tx.ID = ty.ID
I had to requery the form in the after update event of the first combo.
SELECT x.*,
y.*
FROM (SELECT t1.user,
t1.friend
FROM t1
WHERE (( ( t1.user ) = [forms]![myForm]![combo5] ))) AS x
INNER JOIN (SELECT user,
friend
FROM t1
WHERE (( ( t1.friend ) = [forms]![myForm]![combo7] ))) AS y
ON ( x.user = y.user )
AND ( x.friend = y.friend );

Related

Cross check one's manager

I have a table with 2 columns: employee_email, manager_email - which indicate who is manager of a particular employee.
I want to avoid cases that at one row, A is B's manager; but in another row, B is A's manager.
I tried to use the following but it doesn't seem right
select *
from chart c1, chart c2
where c1.employee_email = c2.manager_email
and c1.manager_email = c2.employee_email
Thank you
Is this you need:
select * -- select columns here
from chart c1 left join chart c2
on c1.employee_email = c2.manager_email
and not exists (
select 1 from chart c
where c.manager_email = c1.employee_email
and c.employee_email = c2.manager_email
);

Combine 2 requirements in 1 query

I have an existing PHP project I need to edit. There is also a database with some content.
Some of the tables are organized as follows:
Table: tab_col
Col1
Col2
Col3
...
Table checkbox
id column
1 Col1
2 Col4
...
One of the tasks is to displays all columns of tab_col. Next to them an input field needs to be placed. If the name of one of the columns from tab_col exists in the table checkbox, a checkbox is placed next to the name of the column. Otherwise it is a textfield.
So in this case, a checkbox would be placed next to Col1 and Col4.
This is how it should look like: https://jsfiddle.net/e7Lpfs9f/
Actually, I'm only interested in the SQL query.
With this query, I get all columns:
SHOW columns FROM tab_col
But how can I combine both requirements in only 1 query? I don't want to query the database twice.
PS: I know, this may not be the best way to store data in the database, but that's what I have to work with.
You can do
SELECT t1.column_name, ch.id
FROM information_schema.Columns t1
LEFT JOIN checkbox ch
ON t1.column_name = ch.column
WHERE t1.table_name = 'tab_col'
ORDER BY t1.column_name;
If ch.id is NULL then the column isn't in checkbox
You can address this with stored procedure
WITH CTE1 (
SELECT column_name
FROM [DB_NAME].INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'tbl_col'
)
WITH CTE2 (
SELECT column_name
FROM [DB_NAME].INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'checkbox'
)
SELECT c1.column_name,
CASE ISNULL(c2.column_name,'0')
WHEN '0' THEN 'checkbox'
ELSE 'textbox'
END
FROM CTE1 c1
LEFT JOIN CTE2 c2 on c2.column_name = c1.column_name
Thus if there is a match between tbl_col table and checkbox table is it textbox, otherwise it is checkbox

Why am i getting "Subquery returns more than 1 row"

Hi I am making a webrowser game and I am trying to get monsters into my data base when I get the error:
Subquery returns more then 1 row
here is my code
INSERT INTO monster_stats(monster_id,stat_id,value)
VALUES
( (SELECT id FROM monsters WHERE name = 'Necroborg!'),
(SELECT id FROM stats WHERE short_name = 'atk'),
2);
any ideas how to fix this problem?
Try use LIMIT 1
INSERT INTO monster_stats(monster_id,stat_id,value) VALUES ((SELECT id FROM monsters WHERE name = 'Necroborg!' LIMIT 1),(SELECT id FROM stats WHERE short_name = 'atk' LIMIT 1),2);
Or you could use Insert from select, with join, if you have relations with 2 tables.
INSERT INTO monster_stats(monster_id,stat_id,value)
(SELECT monsters.id, stats.id, 2 as value FROM monsters
LEFT JOIN stats on monsters.id = stats.monsters_id
WHERE monsters.name = 'Necroborg!'
AND stats.short_name = 'atk'
)
MYSQL insert from select:
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
The problem is one or both of the following:
There is more than one monster named 'Necroborg!'.
There is more than on stat named 'atk'.
You need to decide what you want to do. One option (mentioned elsewhere) is to use limit 1 to get only one value from each statement.
A second option is to better specify the where clause so you get only one row from each table.
Another is to insert all combinations. You would do this with insert . . . select and a cross join:
INSERT INTO monster_stats(monster_id, stat_id, value)
SELECT m.id, s.id, 2
FROM (SELECT id FROM monsters WHERE name = 'Necroborg!') m CROSS JOIN
(SELECT id FROM stats WHERE short_name = 'atk');
A third possibility is that there is a field connecting the two tables, such as monster_id. But, based on the names of the tables, I don't think that is true.

selecting data from 2 tables where multiple columns and rows intersect with 'X'

its tricky to explain what i am trying to do so here goes
I have 2 tables derived from a single excel spreadsheet. the spreadsheet defines causes and effects.It has 3 points of reference so i cant make it into 1 table
this spreadsheet has a list of events down the left and effects accross the top. these are associated with each other by inserting an X in the row/column matching the event to the effect. So the intersecting columns/rows form a matrix. So for eg an event could be. 'turn on lightswitch'. follow this row along untill 'X' is found. then follow column up to effects and it shows 'light turns on' (its a little more complicated than that in reality) there can be multiple effects defined by X's in the same row
My idea was in the where statement to use table1.* and table2.* but I was looking for a wildcard to search rows/columns but after research this is not possible in mysql. Using OR for all the furter row/column combinations wont work because it just shows everything with an X in
I am a bit stumped how to query the X row/column part of the table so only the 'example' result part of the query is displayed after searching all the X's A single query below is successful but i need to search table1.1-60 table2.2-61 for occurrances of 'X' by only changeing the '%example%' part of the query in a webpage mysql/php type form and then displaying the results on another page
any suggestions/alternatives welcome thanks
SELECT
table1.equipment,
table1.tagno,
table2.equipment,
table2.action,
table2.service,
table2.tagno
FROM
table1 ,
table2
WHERE
table1.tagno LIKE '%example%' AND
table1.1 = 'X' AND
table2.2 = 'X'
There is no natural way to join column N from table1 with column N+1 in table2; you'll have to specify the joins "by hand" (or in a program, both of which will be ugly).
You want:
select * from table1 t1, table2 t2
where t1.tagno like '%example%' AND t1.1 ='X' and t2.2 = 'X'
UNION
select * from table1 t1, table2 t2
where t1.tagno like '%example%' AND t1.2 ='X' and t2.3 = 'X'
UNION
-- ...
This could be very inefficient. As suggested in the comments, remodelling the data would give you cleaner queries and a happier future.
To give more insight into my suggestion, I would re-model the data like so:
CREATE TABLE effects (
id INT,
name VARCHAR(100)
);
CREATE TABLE events (
id INT,
name VARCHAR(100)
);
CREATE TABLE effects_events (
effect_id INT,
event_id INT
);
effects_events is a Junction Table
So you could query it like this:
SELECT * FROM events WHERE events.id = effects_events.event_id AND effects_events.effect_id = effects.id AND effects.name = 'light turns on';
Or:
SELECT * FROM effects WHERE effects.id = effects_events.effect_id AND effects_events.event_id = events.id AND events.name = 'turn on lightswitch';
I might be misunderstanding something in your question but this seems like the most straightforward solution to me.

Right way to do this query

I have a table
form (
int id )
webformsystemflags ( int id, int formid int sysflagid )
sysflag ( int id, name char(10) )
form table is a table which has all the forms
webform is a table which has the forms which have flags applied to it. It has a foreign key formid which is id to the form table and sysflagid which is foreign key to the sys flag table
sys flag is the table which contains the flags. Lets say I have flags defined as 1,2,3
I can have forms which don't have all the flags applied to it, some may have 1, some may have 2 or some may have 3 applied to it or some may have none.
How can I find all the forms which have either flag 1 or flag 2 or flag 3 applied to it ?
This is a common trick to find EXCLUSION. The value I have below of "FlagYouAreExpectingTo_NOT_Exist" is explicitly the one you expect NOT to be there. Here's how it works.
Get every form and LEFT JOIN to the Web System Flags table WITH FINDING the matching form, and flag setting you DO NOT want. If it finds a valid entry for the form and flag, the "formid" in the (wsf) table will exist. So, we want all that DON'T exist, hence the closing WHERE wsf.formid is null.
It will be NULL for those where it is NOT already flagged.
select
f.ID
from
forms f
left join webformsystemflags wsf
on f.id = wsf.formid
AND wsf.sysflagid = FlagYouAreExpectingTo__NOT__Exist
where
wsf.formid is null
You could use a subquery:
SELECT * FROM `form` WHERE `id` IN (SELECT `formid` FROM `webformsystemflags`)
Careful with subqueries on huge databases though. You could do the same thing with joins but this is an easy solution that will get you going.
Or for all results that DO NOT have a certain flag:
SELECT * FROM `form` WHERE `id` IN (SELECT `formid` FROM `webformsystemflags` WHERE `sysflagid` != 1 OR `sysflagid` != 2)
or a join method:
SELECT f.*, r.`sysflagid` FROM `form` f LEFT JOIN `webformsystemflags` r ON r.`formid` = f.`id` WHERE r.`sysflagid` != null
will get you the forms and the related flags. However, it will not get ALL flags in one row if the form has multiple flags on it. That one you may need to do a concat on the flags, but this answer is already growing unnecessarily complex.
*LAST EDIT *
Ok nutsandbolts - You need to update your question cause the two of us have overshot ourselves in a number of different queries and it isn't really helping to come back saying it doesnt give the right results. The right results can easily be reached by simply examining the queries we have provided and using the general logic behind them to compose the query that is right for you.
So my last suggestion - you say you want a query that will return a form IF it has a certain flag applied to it AND that is does NOT have other flags applied to it.
Here it is supposing you wanted all forms with a flag of 1 AND NOT 2 or 3 or none:
SELECT f.*, r.`sysflagid` FROM `form` f LEFT JOIN `webformsystemflags` r ON r.`formid` = f.`id` WHERE r.`sysflagid` =1 AND r.`formid` NOT IN (SELECT `formid` FROM `webformsystemflags` WHERE `sysflagid` = 2 OR `sysflagid` = 3)
Because your webformsystemflags is relational this query will NOT return any forms that do not exist in the webformsystemflags table - so you don't need to consider null.
If this is not what you're looking for I strongly suggest you rewrite your question with absolute and perfect clarity on your needs cause after this one I'm out of this conversation. Much luck to you though. Have fun.
You can use an exists clause to pull records like this:
select a.*
from form a
where exists (select 1
from webformsystemflags
where formid = a.id
and sysflagid IN (1,2,3))
This won't give you the associated flag. If you want that:
select a.*, b.sysflagid
from form a
join (select formid, sysflagid
from webformsystemflags
where sysflagid in (1,2,3)) b
on a.id = b.formid
There are many different ways to solve this.
EDIT: By reading a comment on the other answer it seems the question was unclear. You want the result forms that only have ONE flag? i.e. the form has flag 1 but not 2 or 3?
edit2: if you really just want a true/false query pulling only the true (has a flag):
select a.*, b.sysflagid
from form a
join webformsystemflags b on a.id = b.formid
If you want forms without flags:
select a.*
from form a
left join webformsystemflags b on a.id = b.formid
where b.formid is null
edit3: Based on comment, forms with one flag and not one of the others:
select a.*
from form a
where exists (select 1 from webformsystemflags where formid = a.id and sysflagid = 1)
and (
not exists (select 1 from webformsystemflags where formid = a.id and sysflagid = 2)
or
not exists (select 1 from webformsystemflags where formid = a.id and sysflagid = 3)
)