Can you explain me, why this code not working?
SELECT SUM(`cash`) AS `cash`,COUNT(*) AS `rows` FROM `table_1` WHERE `login` = 'test' UNION ALL SELECT COUNT(*) AS `rows2` FROM `table_2` WHERE `login` = 'test';
In phpMyadmin I see this message:
1222 - The used SELECT statements have a different number of columns
And I could not solve this problem.
A UNION takes the results of multiple SELECT statements and presents them as a single result set. But in order to do this, the number of columns in the individual SELECT statements has to be the same.
To understand this, it may help to format the query a bit:
SELECT
SUM(`cash`) AS `cash`,
COUNT(*) AS `rows`
FROM `table_1`
WHERE `login` = 'test'
UNION ALL
SELECT
COUNT(*) AS `rows2`
FROM `table_2`
WHERE `login` = 'test'
Your first query is selecting two columns, cash and rows. The second query only selects one column, rows2. Also note that since UNION is concatenating the results, you may as well call the corresponding columns in each query by the same name.
If you really don't have any values that you want to select from the second table, you can substitute a default value for the missing columns:
SELECT
SUM(`cash`) AS `cash`,
COUNT(*) AS `rows`
FROM `table_1`
WHERE `login` = 'test'
UNION ALL
SELECT
NULL AS `cash`
COUNT(*) AS `rows`
FROM `table_2`
WHERE `login` = 'test'
Related
So let's say that I have a query:
SELECT `id` FROM `tablename`
This will returns some IDs in rows. Now I want to use these IDs to get data from another table with the 'IN' function.
SELECT `somecol` FROM `anothertable` WHERE `parent` IN ( {IDs here} )
I could do this with PHP using 2 different queries. But I wanted to know how or can it be done with MySQL alone, using only one query?
Use exists:
SELECT somecol
FROM anothertable a
WHERE EXISTS (
SELECT * FROM tablename t WHERE t.ID = a.parent
);
Just pass in your first query inside you second query:
SELECT
`somecol`
FROM
`anothertable`
WHERE
`parent`
IN (SELECT `id` FROM `tablename`)
BubQuery always return the empty result if the query is correct.
1st Query:
SELECT * FROM `user` WHERE user_age IN(1,22,34);
Result:
2nd Query:
SELECT GROUP_CONCAT(user_age_list) AS user_age FROM `user_detail` WHERE id='1';
Result:
I am try:
SELECT * FROM `user` WHERE user_age IN(SELECT GROUP_CONCAT(user_age_list) AS user_age FROM `user_detail` WHERE id='1');
Sqlfiddle: http://sqlfiddle.com./#!9/d6515f/3 //This is a sample table.
Above the query is always return the empty rows.
But each of query return the result if its run single.
Really I don't know where is the error.
Please update the answer or suggest me.
Avoid Use of GROUP_CONCAT
SELECT *
FROM `user`
WHERE user_age IN(SELECT user_age_list FROM `user_detail` WHERE id='1');
UPDATED
SELECT *
FROM `user` u
WHERE EXISTS (SELECT 1 FROM `user_detail` ud WHERE id='1' AND ud.user_age_list = u.user_age)
SELECT *
FROM user
WHERE user_age IN
(SELECT user_age_list user_age
FROM user_detail
WHERE id='1');
You don't need the group concat here. The engine knows the result set is an inclusive list without the group concat.
What it is trying to do is compare the '1,22,34' to each user_age which is why you get no results.
1 <> '1,22,34'
22 <> '1,22,34'
34 <> '1,22,34'
thus no results.
I am using mysql.
I have a table that has a column id.
Let us say I have an input set of ids. I want to know which all ids are missing in the table.
If the set is "ida", "idb", "idc" and the table only contains "idb", then the returned value should be "ida", "idc".
Is this possible with a single sql query? If not, what is the most efficient way to execute this.
Note that I am not allowed to use stored procedure.
MySQL will only return rows that exist. To return missing rows you must have two tables.
The first table can be temporary (session/connection specific) so that multiple instances can run simultaneously.
create temporary table tmpMustExist (text id);
insert into tmpMustExist select "ida";
insert into tmpMustExist select "idb";
-- etc
select a.id from tmpMustExist as a
left join table b on b.id=a.id
where b.id is null; -- returns results from a table that are missing from b table.
Is this possible with a single sql query?
Well, yes it is. Let me work my way to that, first with a union all to combine the select statements.
create temporary table tmpMustExist (text id);
insert into tmpMustExist select "ida" union all select "idb" union all select "etc...";
select a.id from tmpMustExist as a left join table as b on b.id=a.id where b.id is null;
Note that I use union all which is a bit faster than union because it skips over deduplication.
You can use create table...select. I do this frequently and really like it. (It is a great way to copy a table as well, but it will drop indexes.)
create temporary table tmpMustExist as select "ida" union all select "idb" union all select "etc...";
select a.id from tmpMustExist as a left join table as b on b.id=a.id where b.id is null;
And finally you can use what's called a "derived" table to bring the whole thing into a single, portable select statement.
select a.id from (select "ida" union all select "idb" union all select "etc...") as a left join table as b on b.id=a.id where b.id is null;
Note: the as keyword is optional, but clarifies what I'm doing with a and b. I'm simply creating short names to be used in the join and select field lists
There's a trick. You can either create a table with expected values or you can use union of multiple select for each value.
Then you need to find all the values that are in the etalon, but not in the tested table.
CREATE TABLE IF NOT EXISTS `single` (
`id` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `single` (`id`) VALUES
('idb');
SELECT a.id FROM (
SELECT 'ida' as id
UNION
SELECT 'idb' as id
UNION
SELECT 'idc' AS id
) a WHERE a.id NOT IN (SELECT id FROM single)
//you can pass each set string to query
//pro-grammatically you can put quoted string
//columns must be utf8 collation
select * from
(SELECT 'ida' as col
union
SELECT 'idb' as col
union
SELECT 'idc' as col ) as setresult where col not in (SELECT value FROM `tbl`)
I am trying to select two data sets from the same table, instead of doing two queries I am trying to select them both in one call.
First of all I want to:
SELECT COUNT(*) AS `total` FROM `Messages` WHERE `id` = '1';
and the second is:
SELECT COUNT(*) AS `total_read` FROM `Messages` WHERE `id` = '1' AND `read` = '1';
Is there anyway to do this in one query?
SELECT
COUNT(*) total,
SUM(IF(read='1',1,0)) total_read
FROM Messages
WHERE id='1';
I got a mysterious behaviour using DISTINCT on a MySQL table and can't figure it out:
SELECT DISTINCT `deal_hash`,`city_name`
FROM `a`
WHERE `city_name` = 'b'
...will show me the desired output with DISTINCT on deal_hash. I can also add any other column to the select and it will work only in two cases DISTINCT will fail
SELECT DISTINCT `deal_hash`,`deal_link`
FROM `a`
WHERE `city_name` = 'b'
AND
SELECT DISTINCT `deal_hash`,`loaded_at`
FROM `a`
WHERE `city_name` = 'b'
deal_link is a varchar(255) and loaded_at a INT(20).
DISTINCT shows distinct rows (of column values).
PostgreSQL is the only DB I know of that supports DISTINCT ON, applied to a particular column.
select distinct selects distinct rows. It is not specific to the following column.
Try using group by instead:
select deal_hash, min(deal_link)
from a
where city_name = 'b'
group by deal_hash
or
select deal_hash, max(loaded_at)
from a
where city_name = 'b'
group by deal_hash