I have this particular use case where I have to add a string as a particular field one time for a row in an sql result set.
E.g. :-
I have the following result set:-
| id | Name | Year |
| 1 | ABCD | 2017 |
| 2 | EFGH | 2017 |
I have two string { a , b } which I need to embed into this result set as such without affecting the base table:-
| id | Name | Year | String |
| 1 | ABCD | 2017 | a |
| 2 | EFGH | 2017 | b |
How can I do this with php? or better, with sql?
Related
Ok, I have a single MySQL table with the name 'car' and 3 columns.
+----+--------+------------+
| ID | car_id | engine |
+----+--------+------------+
| 1 | 123 | on |
| 2 | 123 | on |
| 3 | 123 | off |
| 4 | 123 | on |
| 5 | 123 | on |
| 6 | 123 | on |
| 7 | 123 | off |
| 8 | 123 | on |
| 9 | 123 | off |
+----+--------+------------+
Now I want to show the trips this car did. The trips would be determined based on car engine start and stop. For example from the above example we can see that user has made 3 trips as total(From on to off). Now What I want is that if there is a query which gives me only 3 results from on to off meaning if somehow the query groups the records by considering a starting point on and ending point off. Is it possible in mysql? or the other way around is doing it manually by fetching all the records and working in arrays?
At the moment I am fetching all the records and doing it manually by looping all the data and doing accordingly But this process is slow.
Can you try it ?
SELECT * from cars WHERE `engine` = 'off' AND id IN(SELECT id+1 FROM `cars` WHERE `engine` = 'on')
A table contains the following data, is using INNODB, has a UNIQUE constraint on position/fk, and doesn't allow NULL for position.
+----+----------+-----+
| id | position | fk |
+----+----------+-----+
| 1 | 1 | 123 |
| 2 | 2 | 123 |
| 3 | 3 | 123 |
| 4 | 4 | 123 |
| 5 | 5 | 123 |
| 6 | 6 | 123 |
| 7 | 7 | 123 |
| 8 | 8 | 123 |
| 9 | 9 | 123 |
| 10 | 10 | 123 |
+----+----------+-----+
PHP receives a request to update the table to the following. The format of the request can be provided how ever is most convenient such as [2,1,4,3,6,5,8,7,10,9] or [{"id":1, "position":2}, ... ], etc.
+----+----------+-----+
| id | position | fk |
+----+----------+-----+
| 1 | 2 | 123 |
| 2 | 1 | 123 |
| 3 | 4 | 123 |
| 4 | 3 | 123 |
| 5 | 6 | 123 |
| 6 | 5 | 123 |
| 7 | 8 | 123 |
| 8 | 7 | 123 |
| 9 | 10 | 123 |
| 10 | 9 | 123 |
+----+----------+-----+
I've confirmed that SET unique_checks=0; will not allow unique checks to be temporarily disabled, and don't wish to actually remove the unique index, update the table, and reapply the unique index.
How can this table be updated?
If there is no simple means to do so, I thought of a couple of options, but don't like them:
Allowing NULL in position. Is there a way to temporarily allow NULL similar to how SET FOREIGN_KEY_CHECKS=0; can disable foreign keys?
First delete all the records and then reinsert them. This might result in performance issues as there are indexes on the table which will need to be recreated.
All I can think is that you need to first change all the positions to some other values that aren't in the range of new position values you ultimately need to set, but are still unique within the rows.
An easy way to do this, assuming your position column is a signed integer, is to set all the positions to their opposite (negative) value. They'll remain unique, but they won't be in the set of the new values.
You can do this in a transaction along with your subsequent updates, so no other concurrent transaction will ever see the negative values.
BEGIN;
UPDATE MyTable SET position = -position;
UPDATE MyTable SET position = 2 WHERE id = 1;
...etc...
COMMIT;
This is a hack. The sign bit of the integer is being used for a purpose other than showing negative numbers.
I have a MySQL query to delete 'near' duplicate rows from a table, and while using test data outside of my project, the query appears to work as intended. When I use the same query with PHP in the project, I get an SQL error. I've been trying all sorts of different combinations of quotes and backticks and I can't seem to get this working.
Any idea what is going on here?
Problem being solved:
This table sometimes will have rows that are nearly identical, with the only exception being the as_of_date column and the total. Only the the most recent date is important, and any older data is no longer needed in this table once newer data comes in.
Table structure with example data:
+----+---------+------+-------------+-------+
| id | account | year | as_of_date | total |
+----+---------+------+-------------+-------+
| 1 | 123 | 2017 | 2017-02-02 | 250 |
| 2 | 123 | 2017 | 2017-11-24 | 790 |
| 3 | 123 | 2018 | 2018-01-30 | 55 |
| 4 | 456 | 2016 | 2016-04-04 | 500 |
| 5 | 456 | 2016 | 2016-10-10 | 300 |
| 6 | 456 | 2017 | 2017-03-12 | 44 |
| 7 | 789 | 2015 | 2015-12-23 | 2000 |
+----+---------+------+-------------+-------+
Expected Outcome:
The desired result is to delete all 'near-duplicate' rows in the table except for the most recent one (as_of_date). So there should only be at most 1 row for any given account and year. The table should look like this after the query is executed:
+----+---------+------+-------------+-------+
| id | account | year | as_of_date | total |
+----+---------+------+-------------+-------+
| 2 | 123 | 2017 | 2017-11-24 | 790 |
| 3 | 123 | 2018 | 2018-01-30 | 55 |
| 5 | 456 | 2016 | 2016-10-10 | 300 |
| 6 | 456 | 2017 | 2017-03-12 | 44 |
| 7 | 789 | 2015 | 2015-12-23 | 2000 |
+----+---------+------+-------------+-------+
The query:
$query = "DELETE FROM `my_table` AS t
WHERE t.as_of_date NOT IN (
SELECT MAX(as_of_date)
FROM (SELECT * FROM `my_table`) AS t2
WHERE t2.account = t.account AND t2.year = t.year
GROUP BY account, `year`
)";
Here is the SQL error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'AS t
WHERE t.as_of_date NOT IN (
S' at line 1
Don't use table aliases in DELETE FROM. That is, try DELETE FROM my_table WHERE..., omitting AS t.
By the way, the only times you really need backticks are when you have table names that are the same as reserved words or have spaces in them.
SELECT * FROM `SELECT`
or
SELECT * FROM `My Favorite Table`
Wise programmers avoid those situations.
I have a search page that allow user to key in the member ID and it will list out all the downline that belongs to the user.
I am using easyui treegrid to generate the downline.
Now i need to do an extra button to export out all the downline that belongs to the search id and export each line of information into excel file.
This is part of my data, and actually the real data had more column and about 4000++ of data.
Is there anyone can help me or some references? Please let me know if you need more info
+-------------+---------------+---------------------------+------------+
| MemberID | parent_id | Name | Age |
+-------------+---------------+---------------------------+------------+
| 1 | 0 | Cassy | 8 |
| 2 | 1 | Peter | 7 |
| 3 | 1 | Maide | 7 |
| 4 | 1 | Samda | 7 |
| 5 | 4 | Kinso | 7 |
| 6 | 4 | March | 7 |
| 7 | 2 | Sandy | 10 |
| 8 | 0 | Mandy | 12 |
+-------+---------------+----------------------------------------------+
Newbie here.
I'm using codeigniter and mysql
How can I dynamically (number of names may change) convert table from:
+------+-------+-------+
| date | name | value |
+------+-------+-------+
| 06-01| A | 1 |
| 06-02| A | 2 |
| 06-02| B | 3 |
| 06-03| C | 4 |
+------+-------+-------+
To:
+------+---+---+---+
| date | A | B | C |
+------|---+---+---|
| 06-01| 1 | | |
| 06-02| 2 | 3 | |
| 06-03| | | 4 |
+------+---+---+---+
?
Thank you.
Something like this should work.
SELECT date,
SUM(IF(name='A',value,0)) AS 'A',
SUM(IF(name='B',value,0)) AS 'B',
SUM(IF(name='C',value,0)) AS 'C'
FROM myTable
GROUP BY date
ORDER BY date
You need to know what your column names could be to add each of the SUMs manually into your SQL statement, but you could do this using PHP if it was likely to change a lot.
Likewise, replace value with 1 if you just wanted a count of how many times each name appeared, rather than the total of the values in name.