Query within the same table with combination of 2 columns - mysql

I have this table and I need to create an SQL statement WHERE
I need to get ID which content are BOOK with value 05 and content is PAPER with value 'Y',
Result must:
id
1
3

You can group by id, filter the table on your conditions and check in the HAVING clause if both conditions are satisfied:
SELECT id
FROM tablename
WHERE (content, value) IN (('BOOK', '05'), ('PAPER', 'Y'))
GROUP BY id
HAVING COUNT(*) = 2
See the demo.

You can use subquery.
You can select in one query the id-s that meet the condition and use it as a condition
Try:
create table `content`(
id int(9) ,
content varchar (50),
value varchar (10) );
insert into content values (1,'BOOK','05'), (2,'BOOK','05'),
(3,'BOOK','05'), (4,'BOOK','07'), (5,'BOOK','07'), (1,'PAPPER','Y'),
(2,'PAPPER','N'), (3,'PAPPER','Y'), (4,'PAPPER','Y'), (5,'PAPPER','N');
select id from content where content='BOOK' and value ='05' and id in (
select distinct id from content where content='PAPPER' and value ='Y');

Related

MYSQL pivot table dates as rows

I have a table with 3 columns. First column is an identifier, 2nd is value, 3rd is date. Each date has values for many identifiers. I would like to pivot this table in such a way that I have a row for each date and each column has an identifier.
Every answer I find uses CASE to pivot, however in my case I have many (50+) identifiers, and they would change as time goes on.
Here is the code I used, which did not work
with prep as (
select concat(ticker,' ',date) as papel , (pu+pu_juros) as PU, date from table where date >='2021-12-31' and ticker_bench = 'ticker')
select * from prep
pivot (avg(PU) for papel in (select distinct papel from prep))
I tried a second version, defining a table with the values in which to pivot, this also did not work.
with prep_papel as (
select distinct concat(ticker,' ',date) as papel from table where date>='2021-12-31' and ticker_bench = 'ticker'
)
select * from (
select concat(ticker,' ',date) as papel , (pu+pu_juros) as PU, date from table where date >='2021-12-31' and ticker_bench = 'ticker')
as P
pivot (sum(p.PU) for papel in (select * from prep_papel) ) as pv1
Thank you

SELECT in CONCAT

I need to concatenate two columns and I use concat as I see that this function can help me.
An example for concat function is:
SELECT CONCAT(column1,'SEPARATOR',column2) FROM table
And my query is like this:
SELECT
parent_id AS keep_in_mind_parent_id,
(SELECT name FROM table WHERE id = keep_in_mind_parent_id)
FROM
table
WHERE
id = 3
How should I concatenate those columns ? I try it with CONCAT, but doesn't seem to be working.
I'm assuming the table is a tree modeled with parent links (or an "adjacency" model), something like this:
CREATE TABLE `table` (
id INTEGER UNSIGNED PRIMARY KEY,
parent_id INTEGER UNSIGNED NOT NULL,
name VARCHAR(31) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
UNIQUE (parent_id, name)
);
If you want to find the row of a given table that is the parent of another row in the same table, you can INNER JOIN a table to itself on the parent link. Then you can distinguish values in the parent and child tables by the aliases you give in the JOIN. To concatenate the names of the parent and child rows for the child whose id is 3, the join would look like this:
SELECT child.parent_id, child.id AS id,
CONCAT(parent.name, ':', child.name) AS path,
CONCAT(parent.name, ' (#', parent.id, ')') AS parent_name_id
FROM `table` AS child
INNER JOIN `table` AS parent ON child.parent_id = parent.id
WHERE child.id = 3
Then tweak the CONCAT statements to suit the exact format of what you want.
I think you might want group_concat(). Without sample data and desired results, it is hard to tell, but one likely possibility is:
SELECT parent_id AS keep_in_mind_parent_id,
(SELECT group_concat(name) FROM table WHERE id = keep_in_mind_parent_id)
FROM table
WHERE id = 3;

Getting data from column with range of values

I want to get data on particular range of column in a table. but the data is of type varchar.
Suppose i have table student and a column is ID and the value in the ID is as AB12346854, and I need to find all the students in range of id AB12346854 to DE12548847 I tried using the query as
select * from students where id>='AB12346854' and id<='DE12548847'.
I am getting rows with id value as null .( id is not primary key)
You can use this:
select * from students where id BETWEEN 'AB12346854' AND 'DE12548847'
It works for strings too.
select * from test where ID >= 'AA0000001' and ID <= 'DD0000004'
tis is working perfectly fine... even if u ve null values for your ID column

MySQL INSERT INTO table 1 SELECT table 2 with different column name

I have a table (pdt_1) in database (db_1) and another table (pdt_2) in another database (db_2).
I met pdt_1 and pdt_2 to find pdt_1 products not present and published in pdt_2.
functional code :
SELECT * FROM db_1.pdt_1 AS lm
WHERE lm.product_sku
NOT IN (SELECT DISTINCT product_cip7 FROM db_2.pdt_2)
AND lm.product_publish=‘Y'
finally, I need to insert the result of this query in pdt_2.
However, the structure of pdt_1 and pdt_2 are different.
Example:
- columns's names
- columns's numbers
I also need an auto_increment id for pdt_1 products inserted into pdt_2.
I need help.
NB : sorry for my poor english :(
If you want a new table with just the id and product_sku, try:
INSERT INTO new_table # with id and product_sku from first table
SELECT pdt_1.id,
pdt_1.product_sku
FROM db_1.pdt_1
LEFT JOIN db_2.pdt_2
ON pdt_1.product_sku = pdt_2.product_cip7
WHERE pdt_2.product_cip7 IS NULL
AND pdt_1.product_publish = 'Y'

Insert multi column result into single column table

I have a table 'movies' with three Columns: 'id', 'master_id' and 'searchMe' (simplified). I have another Table 'temp_ids' with a single column: 'id'. It is a temporary table, but I don't think that matters.
When I make a query on my table 'movies' like
SELECT `id`, `master_id` FROM 'movies' WHERE searchMe = '1';
I get a multi column result. Now I want to insert every id and every master_id into the 'temp_ids'-Table, but one at a time. So if my result is
id_1 | master_1
id_2 | master_2
id_3 | NULL
I want my temp_ids to look like
id_1
master_1
id_2
master_2
id_3
So I want to convert every single column in the result into its own row. How can I do that in an elegant way?
I know I can do it in multiple queries, searching for id and master_id separatly, and I know I can solve that problem with PHP or so. But I would prefer it to solve that problem in a single mysql-query, if such a thing is possible.
I made a sqlfiddle for this:
http://sqlfiddle.com/#!2/b4a7f/2
To SELECT the data you can use a UNION ALL for this:
SELECT `id`
FROM movies
WHERE searchMe = 1
union all
SELECT `master_id`
FROM movies
WHERE searchMe = 1
and master_id is not null
see SQL Fiddle with Demo
Doing it this way, you cannot distinguish between what value comes from each column, so you can always add an indicator, this will give you two columns but then you know where the data came from:
SELECT `id`, 'id' type
FROM movies
WHERE searchMe = 1
union all
SELECT `master_id`, 'master'
FROM movies
WHERE searchMe = 1
and master_id is not null
Then you would just use this query to INSERT INTO temp using this SELECT
It would be like this
INSERT INTO temp_ids(id)
SELECT id
FROM
(
SELECT id
FROM FirstTable
UNION
SELECT master AS id
FROM SecondTable
) t