Mysql needs to duplicate records using select query - mysql

I have a post table like following,
| ID | TITLE | NUMBER_OF_REPEAT
| 1 | post1 | 2
| 2 | post2 | 1
| 3 | post3 | 3
From the above table I need a select query to produce the row depending upon NUMBER_OF_REPEAT field.
Expected output,
| ID | TITLE
| 1 | post1
| 1 | post1
| 2 | post2
| 3 | post3
| 3 | post3
| 3 | post3
This kind of duplication should support the pagination also. Please help me.
Thanks in advance.

Something like this
SQL Fiddle
create table numbers (number int);
insert into numbers
select 1
union
select 2
union
select 3;
SELECT id, title
FROM tabl
JOIN Numbers
ON tabl.repeater >= Numbers.number
order by id
Its messy,but modify the numbers table for more repeats.
SQL Fiddle

I don't think this should do by DBMS. this can do in programe.
But you can create another table with only two rows and join with post table
let's say table dup with two rows.
SELECT ID ,TITLEfrom post, dup

Related

How to fetch data ( without duplicates records ) from two different tables ? where both tables have common value

Table 1
newpancard
id | name | cardno | status |
-----------------------------
1 | name1| 909099 | done |
2 | name2| 800099 | done |
3 | name3| 965099 | pending|
Table 2
oldpancard
id | name | cardno | status |
-----------------------------
1 | name4| 111119 | done |
2 | name5| 323239 | done |
3 | name6| 734349 | pending|
4 | name7| 609099 | done |
can we get fetch data from both tables where status = done in both tables?
I am trying the following query but getting duplicates data in bulk.
SELECT tb1.*, tb2.*
FROM `newpancard` tb1
JOIN `oldpancard` tb2
ON tb1.status = tb2.status
please correct me. Thanks
I think you actually want a UNION:
SELECT * FROM newpancard WHERE status='done'
UNION
SELECT * FROM oldpancard WHERE status='done'
We use a UNION (rather than UNION ALL) so we don't get duplicate records from newpancard and oldpancard
Output (from your sample data):
id name cardno status
1 name1 909099 done
2 name2 800099 done
1 name4 111119 done
2 name5 323239 done
4 name7 609099 done
SQLFiddle

How to check which ID that has all the different values of another attribute?

I have a table like this:
+--------+--------+
| userID | itemID |
+--------+--------+
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 3 | 4 |
+--------+--------+
I am trying to select all the userID's that has all the different itemID's.
Meaning, if I were to expand itemID's in the future to have itemID 5 too, the same query would select all the userID's that has itemID 3, 4 and 5.
I've struggled with this problem for several hours now, but not managed to find the general query I am looking for.. I would appreciate all the help I could get!
Here is one method:
select userid
from t
group by userid
having count(distinct itemid) = (select count(distinct t2.itemid) from t t2);

Get the count of data based on id in mysql result

I have a table like the below one
id | id_fk | data |
-------------------------
1 | 2 | data1 |
2 | 2 | data2 |
3 | 1 | data3 |
4 | 3 | data4 |
5 | 1 | data5 |
-------------------------
here I have the table id as 'id', foreign key from another table as id_fk.
What I try to achieve is, to get the count of each foreign key in an increment mode. that is, if the id_fk -> 2 occur on the first time, then the count should be 1, at the next occurance count become 2, and so on for all the id_fk. I tried many ways. But none give me the actual output.
From the above table, the result table will look like:
id_fk | count |
------------------
1 | 1 |
1 | 2 |
2 | 1 |
2 | 2 |
3 | 1 |
------------------
Please help me to solve this.. any help will be appreciated.
Try this
SELECT `id_fk`,
#a:=IF(id_fk=#b,#a+1,1) serial_number,
#b:=id_fk
FROM your_table,(SELECT #a:= 0,#b:=0) AS a
ORDER BY `id_fk` ASC
It works perfect with join.
select t1.id_fk,t1.id,count(*)
from your_table t1
left join your_table t2
on t1.id_fk=t2.id_fk and t1.id>=t2.id
group by t1.id_fk,t1.id
See Sql Fiddle Demo

Selecting data from multiple tables with different columns name and remove the duplications in MySQL

I have 3 tables that I want to join
order table
| date | deliver_date |
----------------------------
| 2011-09-01 | 2011-09-13 |
| 2011-09-05 | 2011-09-15 |
deliver table
| deliver_date |
----------------
| 2011-09-01 |
| 2011-09-13 |
return table
|return_date|
--------------
| 2011-09-04 |
| 2011-09-05 |
The data inside 3 tables contains same date.
I want to join the date from 3 tables and display without duplications
The expected result that I want to get something like this
2011-09-01,2011-09-04,2011-09-05,2011-09-13
Can anyone help me?
You need union not join :
select date from order
union
select deliver_date from order
union
select deliver_date from deliver
union
select return_date from return

MySQL query to find duplicate having more than (n) entries

I am having troubles with creating up this sql query to find records having more than (n) entries [n=1] in example
I have table
|--id-|--user_id--|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
want to retrieve duplicates in my table
|--id-|--user_id--|
| 2 | 1 |
| 3 | 1 |
| 5 | 2 |
any help is very much appreciated, thanks for reading though
UPDATE:
I am using Mysql v5.1
This would be my approach
SELECT ID, USER_ID
FROM TABLE
GROUP USER_ID
HAVING COUNT(1) > 1
MINUS
SELECT MIN(ID) ID, USER_ID
FROM TABLE
GROUP BY USER_ID
EDIT: oops, didn't see that you're using MySQL. you might be able to tweak this query to get it working in MySQL
not sure which version of SQL your using but here's the sqlserver answer:
SELECT * from [table_name] GROUP BY user_id HAVING COUNT(*) > n