mysql - How to get data from two tables from database? -
can explain me, why code not working?
select sum(`cash`) `cash`,count(*) `rows` `table_1` `login` = 'test' union select count(*) `rows2` `table_2` `login` = 'test';
in phpmyadmin see message:
1222 - used select statements have different number of columns
and not solve problem.
a union takes results of multiple select statements , presents them single result set. in order this, number of columns in individual select statements has same.
to understand this, may format query bit:
select sum(`cash`) `cash`, count(*) `rows` `table_1` `login` = 'test' union select count(*) `rows2` `table_2` `login` = 'test'
your first query selecting 2 columns, cash , rows. second query selects 1 column, rows2. note since union concatenating results, may call corresponding columns in each query same name.
if don't have values want select second table, can substitute default value missing columns:
select sum(`cash`) `cash`, count(*) `rows` `table_1` `login` = 'test' union select null `cash` count(*) `rows` `table_2` `login` = 'test'
Comments
Post a Comment