replace column value one by another in mysql? - php

I have 10 rows.first 5 rows is gender column with "male".
second 5 rows gender column with "female".
I want mysql query to change where column "male" into "female" and "female" into "male".
Thanks for answering...
If i tried male into female . all fields will be male.

Related

How to Insert new value into table using where

I have one table name "Grade" with this structure:
ID Student_ID First_Name Last_Name Grade
1 1 John Smith 60
2 2 Garry Poul 70
3 1 John Smith 80
And I want to add a new grade for Student_ID = 1 in the table Grade, I am using PHP with MySQL DB.
I used this but gives me error!
$sql = "INSERT INTO Grade (Grade) VALUES ('85') WHERE Student_ID=1 ";
During search I found that I can't use WHERE with INSERT in MySQL, how can solve it?
Thanks for all
For updating existing records use UPDATE and not INSERT
UPDATE Grade
SET Grade = 85
WHERE Student_ID = 1
Insert:
If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query
Update:
when updating records in a table! Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all records in the table will be updated!
UPDATE Grade
SET Grade = 85
WHERE Student_ID=1 ;

SQL: Get unique data between two dates

Hi I am using a mysql table "student" which has 4 columns:
Id Name Date StudentId
1 John 2010-01-15 3
2 Matt 2010-01-10 5
3 Jane 2010-02-10 8
4 John 2010-02-11 3
5 Matt 2010-02-11 5
6 Jane 2010-02-11 8
I want to fetch only new entries in the table between 2010-02-10 and 2010-02-12. If a student had a previous entry in the table then the query should not return that value. So in the above case the query should only return both entries of Jane since John and Matt had an entry each previous to the date specified.
This is what I have but it is not working:
SELECT * FROM student
WHERE date(Date)
between '2010-02-10' and '2010-02-12'
and date(Date)
not between '0000-00-00' and '2015-02-09';
GROUP BY and HAVING is what you are looking for if you want single record per student:
SELECT * FROM student
GROUP BY Name
HAVING DATE(Date) BETWEEN '2010-02-10' AND '2010-02-12';
Or I would use subquery if you want all the records:
SELECT * FROM student
WHERE DATE(Date) BETWEEN '2010-02-10' AND '2010-02-12'
AND Name NOT IN
(SELECT DISTINCT Name FROM student WHERE DATE(Date) < '2010-02-10');
How it works:
the subquery selects all the names that have records prior to the date range, i.e. the ones you don't want in your result. It produces set like ('John', 'Matt'). The main query then selects all the records in the given date range where Name NOT IN ('John', 'Matt').

MySQL closest values order by count

I have a table:
name count
Name 1 1
Name 2 1
Name 3 1
Name 4 1
Name 5 2
Name 6 2
If I select Name 2 (count = 1), how do I select the next and previous item (Name 3, Name 1)? After all values are the same.
How to solve my problem?
If you want a single query then I think this might be the query you are loooking for.
select * from table where coun=(select coun from table where name='name1')
and name!='name1'
I just rename count column to coun as count is an agg. function in mysql.
hope it works for you..

Sql – Insert value in first empty cell of column and show ID

I have a SQL statement that will insert a value into the first empty cell. If I run the PHP script again then it inserts into the next null cell etc.
Problem: I also want to find out the ID of that row, and the value of another column in that row. In the MySQL table below, I want a value inserted in the first ‘null’ of COLUMN A, and also know the ID and value in COLUMN B corresponding to that (i.e. ID= 3 and COLUMN B= 11).
My_TABLE
ID COLUMN A COLUMN B
1 6 78
2 7 90
3 NULL 11
4 NULL 5
5 NULL 123
The following sql statement in PHP script will make it possible to insert value to the first empty cell in COLUMN A:
UPDATE My_TABLE
SET COLUMN A = 83
WHERE COLUMN A IS NULL
LIMIT 1;
Result will be:
ID COLUMN A COLUMN B
1 6 78
2 7 90
3 83 11
4 NULL 5
5 NULL 123
I also want to have an SQL script that will print within PHP (echo) the corresponding values of the ID and COLUMN B of the last updated COLUMN A null value (i.e. ID= 3; COLUMN B= 11).
How do I do that?
Here you go
<?php
// just in case, for comparison
$q = 'SELECT `id`,`colB` FROM My_TABLE WHERE `colA` IS NULL ORDER_BY `id` LIMIT 1';
$row = $pdo->query($q)->fetch(PDO::FETCH_ASSOC);
if ($row) {
// voialà, the value for COLUMN B is now known
list($id, $colB) = $row;
$q = 'UPDATE My_TABLE SET `colA`=? WHERE `id`=?';
$stmt = $pdo->prepare($q);
// voilà, your row is updated
$stmt->execute(array($valueForColumnA,$id));
}
Beware, do not copy and paste whis nilly-willy. Read it, look at it, smell it and understand it. Any questions?

PHP Mysql trying to pull multiple rows from different tables (works for 3 rows on 2 tables but not 4 rows from 3 tables)

I'm trying to pull multiple rows from different tables in a database.
If I only have the first 3 rows from two tables, it works fine. As soon as I add the third table and try and pull another row it breaks!
Ultimately what I'm trying to do is pull the image, the dayid, the name of a person, and then name of a cause.
The imageURL and the Outfitday_id are in the same table, the name of the person is in the table Pilot, and the name of the cause is in the table cause.
For some reason it will pull the first two rows (imageUrl, outfitDay_id) from the table Outfitimage, then pull the 3rd row (name) from Pilot, but fails if I add the name and try and pull it from the Cause table.
$link = mysql_connect("host","user","pass");
if ($link) {
mysql_selectdb("up",$link);
// Select records from the DB
$query = "SELECT imageUrl,outfitDay_id,name,name FROM OutfitImage,Pilot,Cause ORDER BY Rand(" . date("Ymd") . ") LIMIT 1";
$image = mysql_query($query);
// Display records from the table
echo "";
while ($row = mysql_fetch_array($image, MYSQL_NUM)) {
echo "<IMAGE SOURCE='$row[0]'/><br>";
echo "<div id='info'>Day $row[1] of ";
echo "$row[2] Uniform Project for$row[3] </div>";
}
echo "";
} else {
echo "Can't connect to the database!";
}
It probably happens because the name column is ambiguous. I'm guessing you have a name column in both the Cause and Pilot (or some other table). MySQL can't guess what table you want to retrieve the column value from, and will spit an error (Column name in field list is ambiguous).
Make sure you prefix the column name with the table name to remove ambiguity:
SELECT
imageUrl, outfitDay_id, Cause.name, Pilot.name
FROM
OutfitImage, Pilot, Cause
You are attempting a cross-join across all three tables, but you aren't creating any conditions to filter them out. The clause FROM table1, table2, table3 attempts a cross-join across all three tables. A cross-join means every row from one table is paired up with every row from another table. For example, say I have three tables, each have two rows. The first table I call names with the two rows jack and jill, the second table I will call ages with rows 21 and 24, the third table will be genders, and have rows male and female. If I do a clause FROM names, ages, genders, it will return a table with 2 X 2 X 2 = 8 rows.
names ages genders
jack 21 male
jack 21 female
jack 24 male
jack 24 female
jill 21 male
jill 21 female
jill 24 male
jill 24 female
Normally, when you cross-join, you also add a condition to WHERE to filter out rows that don't make sense. For example, from example FROM clause you might have a WHERE genders.id = names.genders_id. In this case, you are linking a name with a specific gender row from the genders table. This will probably also solve your need for a LIMIT 1.

Categories