as the title states, I am trying to query multiple mySQL tables in the same DB simultaneously, here is my query,
SELECT * FROM `database_db`.`one, two, three, four, five, six, seven` WHERE uid='1234567'
I am using mySQL workbench to do the query, but when I run it I get this error
Error Code: 1103
Incorrect table name 'one, two, three, four, five, six, seven'
I know that the problem lies with my query, where I select the tables, I am assuming that I cannot select more than one, or maybe my syntax is incorrect?
Is there another way for me to achieve the same result?
EDIT: Each table is different, each has a different amount of columns, the only thing they have in common is the uid.
For instance, table one has the columns "oneA, oneB, oneC" and table two has the columns "twoA, twoB, twoC, twoD, twoE", and so on and so forth for each table, as you can see they do not have the same amount of columns, and are in no way identical to each other.
Thanx in advance.
You probably want to JOIN the tables:
SELECT *
FROM one
JOIN two ON one.uid = two.uid
JOIN three ON one.uid = three.uid
WHERE one.uid='1234567'
IF table structure is identical for tables: one, two, three ... then you can use UNION
SELECT * FROM one
WHERE uid='1234567'
UNION ALL
SELECT * FROM two
WHERE uid='1234567'
UNION ALL
SELECT * FROM three
WHERE uid='1234567'
....
UNION ALL
SELECT * FROM seven
WHERE uid='1234567'
Maybe I don't understand the question very well but what about something like this:
SELECT *
FROM table_a a, table_b b, table_c c
WHERE a.uid = b.uid = c.uid = 12345;
Related
I have two of same tables with different names. The first one's name is "folders" and the second one is "folders_archive".
I like to INNER JOIN this two tables with thier full content by filtering it on a date. If I use two single query's, thats works fine, but the joined query has no result.
I like something like this:
SELECT *
FROM folders
INNER JOIN folders_archiv
ON folders_archiv.id= folders.id
WHERE folders_archiv.datum = '".$year."-".$month."-".$day."'
AND folders.datum = '".$year."-".$month."-".$day."';
I'm trying to connect these tables in several way, but something is wrong with my logic, please help to fix it.
Thanks.
You question is not very clear, by i think you need Union - gives you all records from two table without repeat(if repeat is acceptable use union all). For example:
select t1.* from (
SELECT column_names
FROM folders
UNION
SELECT column_names
FROM folders_archive) t1
WHERE t1.column_names = what_you_want;
Very vague question.
But try this:
SELECT *
FROM folders
INNER JOIN folders_archiv
ON folders_archiv.datum= folders.datum
WHERE folders_archiv.datum = '".$year."-".$month."-".$day."'
I can't understand whats happening...
I use two sql queries which do not return the same thing...
this one :
SELECT * FROM table1 t1 JOIN table1 t2 on t1.attribute1 = t2.attribute1
I get 10 rows
this other :
SELECT * FROM table1 NATURAL JOIN table1
I get 8 rows
With the NATURAL JOIN 2 rows aren't returned... I look for the missing lines and they are the same values for the attribute1 column ...
It's impossible for me.
If anyone has an answer I could sleep better ^^
Best regards
Max
As was pointed out in the comments, the reason you are getting a different row count is that the natural join is connecting your self join using all columns. All columns are being compared because the same table appears on both sides of the join. To test this hypothesis, just check the column values from both tables, which should all match.
The moral of the story here is to avoid natural joins. Besides not being clear as to the join condition, the logic of the join could easily change should table structure change, e.g. if a new column gets added.
Follow the link below for a small demo which tried to reproduce your current results. In a table of 8 records, the natural join returns 8 records, whereas the inner join on one attribute returns 10 records due to some duplicate matching.
Demo
You need to 'project away' the attribute you don't want used in the join e.g. in a derived table (dt):
SELECT *
FROM table1
NATURAL JOIN ( SELECT attribute1 FROM table1 ) dt;
My Sql Server database has three tables (Amount, wthdrwl, spent), each table has got id, and amount column.
I want to retrieve the amount column from these three different tables.
For both MySQL, SQL-Server, use UNION (implicit distinct) or UNION ALL:
SELECT Amount FROM Amount
UNION ALL
SELECT Amount FROM wthdrwl
UNION ALL
SELECT Amount FROM spent
I suppose there is some relation between three tables
SELECT A.Amount AS Amount ,
W.Amount AS Withdrawal ,
S.Amount AS Spent
FROM Amount A
LEFT OUTER JOIN wthdrwl W ON A.ID = W.ID
LEFT JOIN spent S ON A.ID = S.ID
Check here- SqlFiddle
Given your comment above, use a fixed string to indicate which table the values came from:
SELECT 'Amount' AS source, Amount FROM Amount
UNION ALL
SELECT 'whtdrwl' AS source, Amount FROM whtdrwl
UNION ALL
SELECT 'spent' AS source, Amount FROM spent
then you can easily identify the rows. You could write a query that does this as a single row with 3 columns, but it's far easier doing it this way.
If you want to put them all on one line then you have to have a way to link the columns together via a 2nd column, for example a txnId column that occurs in each of the 3 tables. Then you could do something like:
SELECT Amount.Amount Amt, whtdrwl.Amount Wdrl, spent.Amount Spd
FROM Amount, whthdrwl, spent
WHERE Amount.txnId=wthdrwl.txnId
Amount.txnId=spent.txnId
Use the following query-
select amount.amount, wthdrwl.amount, spent.amount from amount, wthdrwl, spent where amount.amount=wthdrwl.amount and wthdrwl.amount=spent.amount;
Here you have to use the table name along with the attribute to distinctly identify the amount attribute from 3 different table. You need to check for the equality of the id attribute in all 3 tables to correspond to a particular entity in a row of the resultset.
I always get the "the used select statements have different number of columns" error.
select Sublessee_uname, Sublessee_fname, Sublessee_mname, Sublessee_fname from sublessee
union
select Sublessee_space, Sublessee_product from space_reserve
The error is pretty much self-explanatory. In the first SELECT you have 4 columns, whilst in the second you have 2 columns. In order to use UNION, the number of columns in both SELECTs must match.
In Union Case no of columns should be same in query
For you reference check this UNION
In a union the number of column need to match. Try
select Sublessee_uname, Sublessee_fname, Sublessee_mname, Sublessee_fname
from sublessee
union
select Sublessee_space, Sublessee_product, null, null
from space_reserve
Simplified Fiddle example
Based on some of your comments, it seems to me that you need a join of two tables rather than a union thereof.
And often, to join two tables, one of them must have a column or columns referencing the other. As the columns shown in your query do not seem to contain among them the right ones to use in the join condition and I don't know at this point what other columns your two tables possess, I'm going two assume that there's a Sublessee_Id column in both tables. And the query to retrieve the required data would then look something like this:
SELECT
s.Sublessee_uname,
s.Sublessee_fname,
s.Sublessee_mname,
s.Sublessee_fname,
r.Sublessee_space,
r.Sublessee_product
FROM sublessee s
LEFT JOIN space_reserve r /* or, depending on the requirements,
INNER JOIN space_reserve r */
ON s.Sublessee_Id = r.Sublessee_Id
;
If you want to learn more about JOIN syntax, you can try this manual page:
JOIN Syntax (MySQL)
I've a table with fields id_osztaly, id_csoportositas and name (and some other fields but there aren't important).
I want create a query with the follow result: I want combine the name fields. I can't explain so I show an example:
the datas in the table (id_osztaly, id_csoportositas and name order):
1,1,Group1A
1,1,Group1B
1,2,Group2A
1,2,Group2B
I want the follow combine from name:
Group1A-Group2A
Group1A-Group2B
Group1B-Group2A
Group1B-Group2B
(similar the permutation). I can do this with a JOIN, it's ok. But when I three different value of id_csoportositas:
1,1,Group1A
1,1,Group1B
1,2,Group2A
1,2,Group2B
1,3,Group3A
1,3,Group3B
1,3,Group3C
I want:
Group1A-Group2A-Group3A
Group1A-Group2A-Group3B
Group1A-Group2A-Group3C
Group1A-Group2B-Group3A
Group1A-Group2B-Group3B
Group1A-Group2B-Group3C
Group1B-Group2A-Group3A
Group1B-Group2A-Group3B
Group1B-Group2A-Group3C
Group1B-Group2B-Group3A
Group1B-Group2B-Group3B
Group1B-Group2B-Group3C
Yes, it's double join. But I don't know how many different id_csoportositas exist in table. First blick I think I need same number of JOIN as the number of different id_csoportositas.
Is there any trick in (my)sql to do this or should I do it in PHP with a for-cycle?
EDIT maybe I wasn't clear. I know how can I JOIN same table two times or three times. If I've two different id_csoportositas I need only one JOIN. If I've three different id_csoportositas I need two JOIN - I can do this too. But I don't know how many different id_csoportositas exist so I don't know how many JOIN will need. The number of JOINs depends on number of different id_csoportositas and I don't know the number of id_csoportositas without a query.
And I want to group by id_osztaly and different id_osztaly has different id_csoportositas.
I hope it's clear now.
Won't something like this work:
SELECT * FROM
(
(SELECT * FROM table WHERE something = 1) a,
(SELECT * FROM table WHERE something = 2) b,
(SELECT * FROM table WHERE something = 3) c
)
You just select like this and it would provide all possible variations of the joins :)