Row to cloumn based on where condition - Mysql - mysql

I have table with colimns
ID|NAME|AGE
1 |name1|40
1 |name2|45
2 |name3|30
2 |name4|39
result i want like this
ID1|NAME1|AGE1|ID2|NAME2|AGE2
1 |name1|40 | 2 |name3|30
1 |name2|45 | 2 |name4|39
there are around 5k rows.
Thanks.

You can get a full product of the tables:
select table.col1,table.col2,table.col3,table2.col1,table2.col2,table2.col3
from table, table2
where table.col1='test' and table2.col2='test1'
The result may have duplicate records from the both tables. But as you don't have any primary keys that's possibly not an issue for you.

Related

MySQL Fastest Way To Handle Composite Key Query

I'm having some issues with slow queries on a MySQL database with many rows and I'm just hoping to make sure I'm doing this right.
I have a table that contains a TAID with an associated Row like this:
Row | TAID
----------
1 1
2 1
3 1
4 1
1 2
2 2
3 2
4 2
Currently I have TAID, Row setup as a Composite Key, but I generally query all the rows using the TAID column.
Is it slow because there are multiple instances of the TAID?
Am I thinking about this the right way?
Edit: I think the order of the columns is the problem.
I actually have the Row before the TAID and I'm querying on the TAID.
Going to try flipping the order.
As suggested the order of the Composite keys needed to be flipped.
Since I'm querying on the TAID, it needs to be the first key.

Querying in SQL: one to many relationships among fields

I have a table structure as follows - there are two columns A and B. For one value of column A, there can be many values of column B (Corresponding to multiple rows). I want to query SQL in a manner that I get all the values of column A for which corresponding to one particular value of column A, column B does not take a particular value. eg:
A B
1 1
1 2
2 1
2 3
2 4
3 2
3 4
3 5
If I don't want column B to have the value 3 for a particular value of column A, the query should return the following on above data
A
1
3
I cannot figure out how to write such a query and searching manually is too time consuming. Please help me write the query. Thanks in advance.
you question is not very clear. I understand that you want something like
SELECT DISTINCT A FROM table WHERE A NOT IN (SELECT A FROM table WHERE B = 3)

MySQL where condition when intersecting 2 values (comma delimited)

I have table like this:
id products
------ ----------
5 1,2,3
6 2,4,5
9 1,4,7
17 4,6,7
18 1,6,8
19 2,3,6
I have to select only that rows, which row's products column contains one of (2,3) values.
In this case query must return:
id products
------ ----------
5 1,2,3
6 2,4,5
19 2,3,6
But I don't understand how to make construction of this query.
Any ideas?
Thanks in advance.
SELECT id,products
FROM yourTable
WHERE FIND_IN_SET('2',products)>0
OR FIND_IN_SET('3',products)>0
sqlFiddle
Would you mind to try this one please?
select * from TABLE_NAME where products regexp "(^|,)[23](,|$)";
Its doing either two or three at the begining, or at end. Or in between the commas.
Never, never, never store multiple values in one column.
Like you see now this will only give you headaches. Normalize your table. Then you can select normally.
Your table should look like this
id product
-- -------
5 1
5 2
5 3
6 2
6 4
6 5
...
With that table structure your select would be
select id
from your_normalized_table
where product in (2,3)
group by id
having count(distinct product) = 2
That query can make use of indexes and is really fast.

Autoincrement in mysql without unique ids

I have a table with main-ids and user-ids. Each user-id has a set of their own unique main-ids, but multiple user-ids can have the same main-id. Is there anyway to increment a main-id for a specific user without having to do 2 queries?
If you mean like this:
User ID Main ID
1 1
1 2
1 3
2 1
2 2
2 3
Then you're going to need to make an INSERT trigger that finds the next MainID for that user and stores that.

MySQL SELECT query - one cell to multiple rows

In MySQL I have one particular cell with data something like this
5,6,7,8,9
If I need to search for specific 2 numbers one after another I do a query with LIKE statement for those 2 particular numbers. For ex. I need to check if there's a row with numbers 6 & 7 *in a row* I do
SELECT * FROM table WHERE column LIKE '%,6,7,%' OR column LIKE '%,6,7' OR column LIKE '6,7,%'
It's little redundant and clumsy. If I 'convert' those numbers into multiple rows, for ex. every number would become it's own row with column 'numbers' ordered with 'sort' column so I know the order of rows.
id numbers sort
55 8 4
56 6 2
57 5 1
58 7 3
59 9 5
...
What's the identical query for this case? So I would have the same result as with the query above. I need to order the query with sort column and check if the numbers 6,7 are occurring one after another with that sorting.
Should be something like this. (if I understood right your problem). It will return nothing if the 2 numbers are not in sequence by the sort column.
select *
from table t1
join table t2 on t1.sort=t2.sort+1
where t1.numbers=6 and t2.numbers=7
if you do not know which one should be first you can use it like this:
select *
from table t1
join table t2 on t1.sort=t2.sort+1 or t1.sort+1=t2.sort
where t1.numbers=6 and t2.numbers=7
Are the numbery always incremented by one or can they have any value?
I'm proposing the following table structure
id col1 col2 rowid
1 1 2 1
2 2 3 1
3 1 4 2