Mysql ORDER BY FIELD() in specific pattern - mysql

if anyone please help me , i am struck with it..
sorry for my bad english
My sql query is:-
SELECT * FROM table ORDER BY FIELD(ID,1,5,4,3)
I want to do this id 1 should be first record, id 5 should be 6th record, id 4 should be 11th record and id 3 should be 16th record,
Is there a way to do this in mysql..
Please help me.

Your Code should work as long as you only want to view the IDs listed in the order by clause. Try to add the ID you are selecting to the where clause:
SELECT * FROM table
WHERE ID in(1,3,4,5)
ORDER BY FIELD(ID,1,5,4,3) ;
Could you test it? It is working fine for me.
You can also solve this problem with multiple selects and union.
SELECT * FROM table where ID=1
UNION ALL
SELECT * FROM table where ID=5
UNION ALL
SELECT * FROM table where ID=4
UNION ALL
SELECT * FROM table where ID=3 ;
UNION ALL will show a complete resultset. UNION without ALL will exclude duplicate results. In your case duplicates are impossible, so union without ALL would suffice :
SELECT * FROM table where ID=1
UNION
SELECT * FROM table where ID=5
UNION
SELECT * FROM table where ID=4
UNION
SELECT * FROM table where ID=3 ;

Related

How do I select some rows and then others in MySQL?

I have this query, it works but I'm not sure if it's the best approach and I don't get what I want.
I need to select the query contained in the "IN" clause first, then union with others. Entire row returned must be 40.
SELECT *
FROM (
SELECT * FROM tbl_x a WHERE id IN(11,20,30)
UNION ALL
SELECT * FROM tbl_x b WHERE exam_group='jpx' AND subject='chemistry'
) ab
GROUP BY id LIMIT 40
The next query should to return same data in simple way:
SELECT *
FROM tbl_x
WHERE
id IN (11,20,30)
OR (exam_group='jpx' AND subject='chemistry')
ORDER BY id IN (11,20,30) DESC, id
LIMIT 40;

How can I create a view with duplicate columns and merge them?

I have pm_message tables with 1~9, and I want to create a view to simplified the process of MySQL query.
What I have is
CREATE VIEW `pm_messages` AS
SELECT * FROM
`pm_messages_0`,
`pm_messages_1`,
`pm_messages_2`,
`pm_messages_3`,
`pm_messages_4`,
`pm_messages_5`,
`pm_messages_6`,
`pm_messages_7`,
`pm_messages_8`,
`pm_messages_9`;
I got error with douplicate column. There is no record is duplicate, I want to merge all of them in view, what should I do?
You have coded a colossal cross join. Depending on the number of rows, it probably wouldn't return before the universe suffers entropy heat death.
I'm almost certain you want a union:
CREATE VIEW `pm_messages` AS
SELECT * FROM `pm_messages_0` union all
SELECT * FROM `pm_messages_1` union all
SELECT * FROM `pm_messages_2` union all
SELECT * FROM `pm_messages_3` union all
SELECT * FROM `pm_messages_4` union all
SELECT * FROM `pm_messages_5` union all
SELECT * FROM `pm_messages_6` union all
SELECT * FROM `pm_messages_7` union all
SELECT * FROM `pm_messages_8` union all
SELECT * FROM `pm_messages_9`;
This will work if all tables have the same number and type of columns. If not, you'll have to explicitly select columns such that each select returns the same number and type of columns.

How to select all entries of a MySQL table except the last one?

How to select all entries but the last one in MySQL?
I tried the following statement:
SELECT *
FROM table name
ORDER BY id
SELECT id FROM lot_master WHERE `id auto increment` ORDER BY id decs LIMIT 1
How can I achieve this?
select Name from shopping_cart where id<>(select max(id) from shopping_cart);

Select a value from MySQL database only in case it exists only once

Lets say I have a MySQL table that has the following entries:
1
2
3
2
5
6
7
6
6
8
When I do an "SELECT * ..." I get back all the entries. But I want to get back only these entries, that exist only once within the table. Means the rows with the values 2 (exists two times) and 6 (exists three times) have to be dropped completely out of my result.
I found a keyword DISTINCT but as far as I understood it only avoids entries are shown twice, it does not filters them completely.
I think it can be done somehow with COUNT, but all I tried was not really successful. So what is the correct SQL statement here?
Edit: to clarify that, the result I want to get back is
1
3
5
7
8
You can use COUNT() in combination with a GROUP BY and a HAVING clause like this:
SELECT yourCol
FROM yourTable
GROUP BY yourCol
HAVING COUNT(*) < 2
Example fiddle.
You want to mix GROUP BY and COUNT().
Assuming the column is called 'id' and the table is called 'table', the following statement will work:
SELECT * FROM `table` GROUP BY id HAVING COUNT(id) = 1
This will filter out duplicate results entirely (e.g. it'll take out your 2's and 6's)
Three ways. One with GROUP BY and HAVING:
SELECT columnX
FROM tableX
GROUP BY columnX
HAVING COUNT(*) = 1 ;
one with a correlated NOT EXISTS subquery:
SELECT columnX
FROM tableX AS t
WHERE NOT EXISTS
( SELECT *
FROM tableX AS t2
WHERE t2.columnX = t.columnX
AND t2.pk <> t.pk -- pk is the primary key of the table
) ;
and an improvement on the first way (if you have a primary key pk column and an index on (columnX, pk):
SELECT columnX
FROM tableX
GROUP BY columnX
HAVING MIN(pk) = MAX(pk) ;
select id from foo group by id having count(*) < 2;

mysql UNION result has less rows than sum of table row counts

I'm using UNION to get all names in different tables.
my tables has about 10000 rows all together.
but the query returns 468 rows!!
My query is:
SELECT name FROM `shopping`
UNION
SELECT name FROM stores
UNION
SELECT name FROM concert
UNION
SELECT val AS name FROM event
UNION
SELECT name FROM fastfood
Where is the problem?
UNION removes duplicate values. You probably want UNION ALL instead.